[R] Basic aggregate help

rkevinburton at charter.net rkevinburton at charter.net
Thu Oct 9 19:14:02 CEST 2008


>From what I read this should work. So please help my misunderstanding:

> x <- data.frame(Name=c("A","A","C"), Category=c("a","a","b"), Quantity=c(1,2,3))
> x
  Name Category Quantity
1    A        a        1
2    A        a        2
3    C        b        3

> aggregate(x, by=as.list(setdiff(names(x),"Quantity")), sum)
Error in FUN(X[[1L]], ...) : arguments must have same length

The way I understand it is that 'aggegate' should group the rows that are the same (in the above 1,2 are the same and 3 stands alone) and the FUN should be applied to the ramaining column. I was expecting a data.frame like:

  Name Category Quantity
1   A        a          3
2   C         b         3

But that is not what I got. Would someone be so kind as pointing out what I am doing wrong?

Thank you.

Kevin



More information about the R-help mailing list