[R] Variable Column Names and Lists

Brian Trautman btrautman84 at gmail.com
Fri Feb 27 19:18:15 CET 2015


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]]



More information about the R-help mailing list