[R] Variable Column Names and Lists

Brian Trautman btrautman84 at gmail.com
Fri Feb 27 22:35:35 CET 2015


This worked perfectly, thank you!

On Fri, Feb 27, 2015 at 10:47 AM, Duncan Murdoch <murdoch.duncan at gmail.com>
wrote:

> On 27/02/2015 1:18 PM, Brian Trautman wrote:
> > 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
>
> I don't know the data.table function, but
>
>  list(sumvar = sum(get(bvar)))
>
> will produce a list with one element named sumvar, holding the sum of
> values of a variable named 'b'.  If you want that element to be named
> 'mysum', you could change it at the end, or construct that list as
>
> structure(list(sum(get(bvar))), names=sumvar)
>
> i.e. the full expression would be
>
> dt_min <- dt[, structure(list(sum(get(bvar))), names=sumvar), by=list(a)]
>
> The idea is that structure() produces list(sum(get(bvar))) with
> attribute "names" set to the contents of sumvar.
>
> Duncan Murdoch
>
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list