Reputation: 1304
I have a data table that looks something like this, and I want to apply to it function f
library(data.table)
dt <- data.table(id= c(1,1,1,2,2,2,3,3,3), year=c(1,2,3,1,2,3,1,2,3),y = rnorm(9), x1 = rnorm(9), x2 = c(0,0,0,0,1,0,1,1,1),c2 = rnorm(9))
f <- function(data, var1, var2){
data[,(paste("times",var1,var2, sep = "_")) := get(var1)*get(var2)]
}
I would like to apply f
over multiple variables obtaining results similar to the one below:
vars<-c("x1","x2","c2")
dt2<-lapply(vars, function(x) f(dt,"y", x))
dt2<-do.call(rbind,dt2)
I would like to obtain the same results using a for loop instead of lapply
, unfortunately, I am not good with loops. Could anyone help me?
so far I tried the following but it does not work properly.
for(i in vars) {
dt<-f(dt,"y",i)
}
Thanks a lot in advance for your help
Upvotes: 0
Views: 528
Reputation: 389047
There could be better ways to solve this but since we don't know much about the nature of the exact problem here is a for
loop attempt which reproduces the same result as lapply
.
library(data.table)
list_df <- vector('list', length(vars))
for(i in seq_along(vars)) list_df[[i]] <- f(dt,'y',vars[i])
rbindlist(list_df)
# id year y x1 x2 c2 times_y_x1 times_y_x2 times_y_c2
# 1: 1 1 -0.5605 -0.446 0 0.701 0.2498 0.000 -0.3931
# 2: 1 2 -0.2302 1.224 0 -0.473 -0.2818 0.000 0.1088
# 3: 1 3 1.5587 0.360 0 -1.068 0.5608 0.000 -1.6644
# 4: 2 1 0.0705 0.401 0 -0.218 0.0283 0.000 -0.0154
# 5: 2 2 0.1293 0.111 1 -1.026 0.0143 0.129 -0.1326
# 6: 2 3 1.7151 -0.556 0 -0.729 -0.9533 0.000 -1.2501
#...
#...
Upvotes: 2