Let's assume I have the following code:
x = data.table(rowid=1:10, N1=2:11, N2=3:12, N3=4:13)
x[, sum(c(N1, N2, N3)),by=rowid]
Now assume I don't know the column names N1, N2, N3 in advance and they are saved in the vector colnames=c("N1", "N2", "N3"). If I know the number of variables to pass to sum(), I can plug in colnames as follows:
x[, sum(c(get(colnames[1]),get(colnames[2]),get(colnames[3]))),by=rowid]
Now assume I don't know the length of colnames. Is there any way I can rewrite the above so it works? Something like x[, sum(c(sapply(colnames, as.name))),by=rowid] (note this exact expression doesn't work).