This should be a simple question, but I am at my wits end.

dt<-data.table(a=rep(1:10, 26), b=1:260, c=rep(1:2, 130))

sumvar <- 'mysum'
bvar <- 'b'

dt_min <- dt[, list(sumvar = sum(get(bvar))), by=list(a)]
print(dt_min)

I want the function to return two variables, "a" and "mysum".  However, it
instead returns "a" and "sumvar", rather than evaluating "sumvar".

If I try replacing it with "get(sumvar)" or "eval(sumvar)", R just throws
an error.  What am I missing?

     a sumvar
 1:  1   3276
 2:  2   3302
 3:  3   3328
 4:  4   3354
 5:  5   3380
 6:  6   3406
 7:  7   3432
 8:  8   3458
 9:  9   3484
10: 10   3510

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to