[datatable-help] Expressions in "by" criteria (again)

mdowle at mdowle.plus.com mdowle at mdowle.plus.com
Fri Jul 2 18:49:16 CEST 2010


Quick answer is it needs to be this way :

   my.fields = quote(list(Apples,Bananas,...))
   DT[,sum(NumericField),by=eval(my.fields)]

Also some bugs were just fixed in this area so you may need latest 1.5
from r-forge for this.

Having said that its sometimes easier coding to use a flat format (i.e.
have a single column 'fruit') then "[,...,by=fruit]". There was another
thread showing examples of long to wide taking care of NAs etc, search for
'wide'.

HTH, thanks for the interest,

Matthew


> Hi,
>
> I suspect my question is similar to Harish's "Question #2" from 6/18.
> Suppose
> I have a data.table with many fields and have a large subset of fields I
> need to include
> in several expressions. Ordinarily, I would create (once) a vector of
> names of the fields
> in my subset:
> my.fields <- c("Apples","Bananas","Coconuts","Dragonfruits",...,"Pomelos")
>     [where the whole data frame has many more fields, including
> "Broccoli","Cabbages",...]
>
> Then I can re-use the my.fields vector when extracting subsets, creating
> plots, aggregating with
> ddply(), etc. The problem is that I can't figure out how to
> (re)use my.fields to aggregate a
> data.table.
>
> DT[,sum(NumericField),by=(Apples,Bananas,Coconuts,Dragonfruits,...,Pomelos)]
> will work.
> However,
> DT[,sum(NumericField),by=my.fields]
> won't work, nor will any combination of paste(), list(), eval(), quote(),
> deparse(), etc. applied
> to my.fields (at least I haven't found one yet).
>
> I know this is probably more an R-language issue, but since it's come up
> in my work with
> the (excellent) data.table package, I thought I would ask here.
>
> Thanks!
> Mike S.
>
>
>
> _______________________________________________
> datatable-help mailing list
> datatable-help at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
>




More information about the datatable-help mailing list