[R] Basic aggregate help

jim holtman jholtman at gmail.com
Thu Oct 9 19:21:30 CEST 2008


Is this what you want:

> x <- data.frame(Name=c("A","A","C"), Category=c("a","a","b"), Quantity=c(1,2,3))
> aggregate(x$Quantity, list(x$Name, x$Category), FUN=sum)
  Group.1 Group.2 x
1       A       a 3
2       C       b 3
>

'by' is "a list of grouping elements, each as long as the variables in
x."  Your version was just sending in the names, not the values.

On Thu, Oct 9, 2008 at 1:14 PM,  <rkevinburton at charter.net> wrote:
> >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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list