[R] "aggregate" in R
jim holtman
jholtman at gmail.com
Wed Feb 23 00:06:55 CET 2011
try data.table:
x a b
1 1 1 21
2 2 2 22
3 3 3 23
4 4 4 24
5 5 5 25
6 1 6 26
7 2 7 27
8 3 8 28
9 4 9 29
10 5 10 30
11 1 11 31
12 2 12 32
13 3 13 33
14 4 14 34
15 5 15 35
> require(data.table)
Loading required package: data.table
Quick start guide : vignette("datatable-intro")
Homepage : http://datatable.r-forge.r-project.org/
> y <- data.table(y)
> y[, list(mean = mean(a), sum=sum(b)), by = x]
x mean sum
[1,] 1 6 78
[2,] 2 7 81
[3,] 3 8 84
[4,] 4 9 87
[5,] 5 10 90
On Tue, Feb 22, 2011 at 5:25 PM, Hongwei Dong <pdxdong at gmail.com> wrote:
> Hi, R users,
>
> I'm wondering how I can aggregate data in R with different functions for
> different columns. For example:
>
> x<-rep(1:5,3)
> y<-cbind(x,a=1:15,b=21:35)
> y<-data.frame(y)
>
> I want to aggregate "a" and "b" in y by "x". With "a", I want to use
> function "mean"; with "b", I want to use function "sum". I tried:
>
>> aggregate(y,x,mean(y$a),sum(y$b))
>
> But I got the error:
>
> Error in match.fun(FUN) :
> 'mean(y$a)' is not a function, character or symbol
>
>
> Anyone can tell me how to fix this problem? Thanks.
>
> Gary
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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
Data Munger Guru
What is the problem that you are trying to solve?
More information about the R-help
mailing list