0

I'd like to reshape my data set using data.table. I have two variables storing the column names of the variables of interest

by_var = "treatment"
mean_var = "decrease"

Here are some data:

dat = as.data.table(OrchardSprays)

and I would like to perform the following operation

dat[,list(mean=mean(decrease)),by=treatment]

How can I indicate which variables to use when the variable names are stored into objects? I am trying to do something like that

dat[,list(mean=mean(mean_var)),by=by_var]

When doing so, by_var seem to work fine but mean_var is not considered as a column name.

Remi.b
  • 17,389
  • 28
  • 87
  • 168
  • You can do `by=byvar`, as you have it. For `mean_var`, you'll want `get(mean_var)`. I think this question may be a dupe; gonna check. – Frank May 12 '15 at 00:01
  • 1
    Thank you! Looks like a duplicate indeed. I am voting to close. – Remi.b May 12 '15 at 00:05

0 Answers0