[R] aggregate and the $ operator

Ed Siefker ebs15242 at gmail.com
Fri Jan 22 20:20:59 CET 2016


Aggregate does the right thing with column names when passing it
numerical coordinates.
Given a dataframe like this:

  Nuclei Positive Nuclei Slide
1    133              96    A1
2     96              70    A1
3     62              52    A2
4     60              50    A2

I can call 'aggregate' like this:

> aggregate(example[1], by=example[3], sum)
  Slide Nuclei
1    A1    229
2    A2    122

But that means I have to keep track of which column is which number.
If I try it the
easy way, it doesn't keep track of column names and it forces me to
coerce the 'by'
to a list.

> aggregate(example$Nuclei, by=list(example$Slide), sum)
  Group.1   x
1      A1 229
2      A2 122

Is there a better way to do this?  Thanks
-Ed



More information about the R-help mailing list