Reputation: 3656
I have read a folder with Excel files with a lapply
function and now have a list with data.tables. The individual data.tables have different dimensions and column names.
Now I have to apply a series of functions on this list to clean it up. Where I get stuck is: how do I apply a column-wise function on every data.table inside this list?
library(data.table)
# make a list of data.tables
set.seed(123)
dt = data.table(x = runif(10), y = runif(10))
dt2 = data.table(a = runif(10), y = runif(10), z = runif(10))
l = list(dt, dt2)
names(l) = c("dt", "dt2")
# sample function to be applied
func = function(x) {
y = x * 2
}
# how it would work for an invidual data.table
dt[, lapply(.SD, func)]
Upvotes: 3
Views: 1131
Reputation: 887951
We loop through the list
with lapply
and apply the function column wise within each list
element.
lapply(l, function(x) x[,lapply(.SD, func)])
Upvotes: 5