pass column name in data.table using variable in a for loop in R -
i want loop through selection of columns data table , summarise data column. i'm able pass column name in variable using quote()
, eval()
, works fine
library("data.table") dt <- data.table(x=c("b","b","b","a","a","c","c"), y = c("x","y","z","x","y","z","x"), v=rnorm(7)) splitby <- quote(y) sdt <- dt[,list(meanv = mean(v)),by=. (eval(splitby))]
however when try put loop this
library("data.table") dt <- data.table(x=c("b","b","b","a","a","c","c"), y = c("x","y","z","x","y","z","x"), v=rnorm(7)) mylist <- c("x","y") (i in mylist){ splitby <- quote(i) sdt <- dt[,list(meanv = mean(v)),by=. (eval(splitby))] }
it doesn't work. know split evaluates correctly x
or y
. doing wrong?
Comments
Post a Comment