[R] Summarizing For Values with Multiple categories

Gabor Grothendieck ggrothendieck at gmail.com
Sun Oct 24 01:12:43 CEST 2010


On Sat, Oct 23, 2010 at 7:03 PM, Alison Waller <alison.waller at embl.de> wrote:
> Thanks!
>
> I tried reading the help for aggregate and can't figure out which form of
> the formula I am using, and therefore the syntax.
>
> I'm getting the below error.
>
>> aggregate(counts ~ ind, merge(stack(CAT2COG), df, by = 1), sum)
> Error in as.data.frame.default(x) :
>  cannot coerce class "formula" into a data.frame
>> aggregate(counts ~ Cats, merge(stack(CAT2COG), df, by = 1), sum)
> Error in as.data.frame.default(x) :
>  cannot coerce class "formula" into a data.frame
>> Cats
> [1] A B C D E
> Levels: A B C D E
>> aggregate(counts ~ COGs, merge(stack(CAT2COG), df, by = 1), sum)
> Error in as.data.frame.default(x) :
>  cannot coerce class "formula" into a data.frame
> On 24-Oct-10, at 12:50 AM, Gabor Grothendieck wrote:
>
>>> aggregate(counts ~ ind, merge(stack(CAT2COG), df, by = 1), sum)
>
>

Maybe you have too old a version of R?

I am using this version:

> R.version.string
[1] "R version 2.11.1 Patched (2010-05-31 r52167)"

Here is a self contained example using the objects you posted:

> CAT2COG<-list(A="COG1",B=c("COG1","COG2"),C=c("COG1","COG3"),D=c("COG2","COG4"))
> df<-data.frame(COGs=c("COG1","COG2","COG3","COG4"),counts=c(10,20,30,40))
> aggregate(counts ~ ind, merge(stack(CAT2COG), df, by = 1), sum)
  ind counts
1   A     10
2   B     30
3   C     40
4   D     60





-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list