0

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

2
  • 1
    It's difficult to help when you do not have a reproducible example for the error Commented May 1, 2020 at 9:29
  • yes, I completely understand, unfortunately, I am not sure what is going wrong with the lapply approach as it works with other data. This is why I am looking for a different approach that uses a for loop. Any answer that would reproduce the results in dt2 of the example using a for loop would work. I will update the question to make it clearer Commented May 1, 2020 at 9:31

1 Answer 1

2

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
#...
#...
Sign up to request clarification or add additional context in comments.

2 Comments

Great thank you very much. it works well!. Can I ask why did you have to use the seq_along() command?
seq_along creates a sequence from 1 to length(vars). We use it here to store the list index number so that we can save each dataframe as list_df[[1]], list_df[[2]] etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.