Open In App

Apply Function to data.table in Each Specified Column in R

Last Updated : 30 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see that how to apply a function to data.table in each specified column in R Programming Language.

The data.table library in R is used to create datasets and represent it in an organized manner. The library can be downloaded and installed into the working space using the following command : 

install.packages("data.table") 

The data table can then be created using the base R data.table method. Row and column names can be specified in the data.table. Each column may belong to a different data type.

A function can be applied to the specific columns of the data.table. The function, be aggregate, or logical can be applied to the data.table. This can be done using the lapply method is used to apply an operation to all the elements of the given input list object.

This method takes as input data frame object and returns the list object as the output. The lapply method has the following syntax in R :

Syntax: lapply(obj, fun)

Arguments:

Obj: A vector or an object
Fun: Function applied to each element of input R object

The method can be applied over particular columns of the specified data frame object. The following code snippet illustrates the procedure where an arithmetic operator is applied to the data values in the specified data frame columns. The constant value 1 is added to the col1 and col3 of the data frame. The result is retained in the original data frame. 

R




# installing the required library
library(data.table)
  
# creating the data table
dt <- data.table (
  col1 = 1:5,
  col2 = c(TRUE,FALSE,FALSE,TRUE,TRUE),
  col3 = c(7,5,2,4,8)
)
  
# print data table
print("Data Table")
print(dt)
  
# specifying the columns to modify
col_vals <- c("col1","col3")
  
# adding 1 to the col1 and col3 value
dt[ , (col_vals) := lapply(.SD, "+", 1), 
   .SDcols = col_vals]
  
# modified data table
print("Modified Data Table")
print(dt)


Output

[1] "Data Table"
   col1  col2 col3
1:    1  TRUE    7
2:    2 FALSE    5
3:    3 FALSE    2
4:    4  TRUE    4
5:    5  TRUE    8
[1] "Modified Data Table"
   col1  col2 col3
1:    2  TRUE    8
2:    3 FALSE    6
3:    4 FALSE    3
4:    5  TRUE    5
5:    6  TRUE    9

The following code snippet illustrates the application of a relational operation of greater than between the data elements of the used data frame. The col1 values of the data frame are compared with the constant value of 3 in a greater than operation. All the columns which satisfy the value return a true boolean value, else false is returned. 

R




# installing the required library
library(data.table)
  
# creating the data table
dt <- data.table (
  col1 = 1:5,
  col2 = c(TRUE,FALSE,FALSE,TRUE,TRUE),
  col3 = c(7,5,2,4,8),
  col4 = c("Geeks","for","Geeks","is","fun")
)
  
# print data table
print("Data Table")
print(dt)
  
# specifying the columns to modify
col_vals <- c("col1")
  
# checking if col1 value is
# greater than the constant 3
dt[ , (col_vals) := lapply(.SD, ">", 3),
   .SDcols = col_vals]
  
# modified data table
print("Modified Data Table")
print(dt)


Output

[1] "Data Table"
   col1  col2 col3  col4
1:    1  TRUE    7 Geeks
2:    2 FALSE    5   for
3:    3 FALSE    2 Geeks
4:    4  TRUE    4    is
5:    5  TRUE    8   fun
[1] "Modified Data Table"
    col1  col2 col3  col4
1: FALSE  TRUE    7 Geeks
2: FALSE FALSE    5   for
3: FALSE FALSE    2 Geeks
4:  TRUE  TRUE    4    is
5:  TRUE  TRUE    8   fun


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads