[R] aggregating using 'with' function

AC Del Re delre at wisc.edu
Sun Feb 21 00:54:12 CET 2010


Hi All,

I am interested in aggregating a data frame based on 2
categories--mean effect size (r) for each 'id's' 'mod1'. The
'with' function works well when aggregating on one category (e.g.,
based on 'id' below) but doesnt work if I try 2 categories. How can
this be accomplished?

# sample data

id<-c(1,1,1,rep(4:12))
n<-c(10,20,13,22,28,12,12,36,19,12, 15,8)
r<-c(.98,.56,.03,.64,.49,-.04,.49,.33,.58,.18, .6,.21)
mod1<-factor(c(1,2,2, rep(c(1,2,3),3)))
mod2<-c(1,2,15,rep(3,9))
datas<-data.frame(id,n,r,mod1,mod2)

# one category works perfect:

with(datas,  aggregate(list(r = r),  by = list(id = id),mean))

  id          r
1   1  0.5233333
2   4  0.6400000
3   5  0.4900000
4   6 -0.0400000
5   7  0.4900000
6   8  0.3300000
7   9  0.5800000
8  10  0.1800000
9  11  0.6000000
10 12  0.2100000

# trying with 2 categories:

 with(datas,  aggregate(list(r = r),  by = list(c(id = id, mod1 = mod1)),mean))

Error in FUN(X[[1L]], ...) : arguments must have same length

Thank you,

AC



More information about the R-help mailing list