Apply Function to data.table in Each Specified Column in R
Last Updated :
30 Aug, 2022
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
Similar Reads
Apply function to each row in Data.table in R In this article, we are going to see how to apply functions to each row in the data.table in R Programming Language. For applying a function to each row of the given data.table, the user needs to call the apply() function which is the base function of R programming language, and pass the required p
1 min read
Apply function to each column of matrix in R In this article, we will explore a method to apply the function to each matrix column by using R Programming Language. How to apply the function to each column of the matrix The function 'apply()' is used to apply the function to each column of the matrix. By using these methods provided by R, it is
3 min read
Shift a column of lists in data.table by group in R In this article, we will discuss how to shift a column of lists in data.table by a group in R Programming Language. The data table subsetting can be performed and the new column can be created and its values are assigned using the shift method in R. The type can be specified as either "lead" or "lag
2 min read
How Do You Delete a Column by Name in data.table in R? Data manipulation is a critical aspect of the data analysis and R's data.table package is a powerful tool for handling large datasets efficiently. One common task is deleting a column by its name. This article will guide us through the process providing examples and best practices to ensure we can m
3 min read
Extract data.table Column as Vector Using Index Position in R The column at a specified index can be extracted using the list sub-setting, i.e. [[, operator. The double bracket operator is faster in comparison to the single bracket, and can be used to extract the element or factor level at the specified index. In case, an index more than the number of rows is
2 min read
How to Aggregate multiple columns in Data.table in R ? In this article, we will discuss how to aggregate multiple columns in Data.table in R Programming Language. A data.table contains elements that may be either duplicate or unique. As a result of this, the variables are divided into categories depending on the sets in which they can be segregated. The
5 min read