I have list of variable names in a vector of strings v and a data table my.dt that contains all these variables.
> v
[1] "var1" "var2" "var3"
I want to use those variables whose names are in the vector v, such that i create new variable that is cbind of these 3, or any number of names that appear in v, like:
new <- cbind(my.dt[,"var1"],my.dt[,"var2"],my.dt[,"var3"])
new1 <- rowSums(new, na.rm=TRUE) * ifelse(rowSums(is.na(new)) == ncol(new), NA, 1)
How can i get this, having in mind that number of variables is not fixed, so i dont want to refer to each element like v[1], v[2] etc.
my.dt[, v]?data.tablebecause you did not mention it. The quality of your post is poor. Next time, please provide relevant information and reproducible example so others can better help you.