Thursday, 15 January 2015

aggregate - Multiple aggregation with arithmetic on LHS in R formula -



aggregate - Multiple aggregation with arithmetic on LHS in R formula -

anybody have bright ideas on how multiple aggregations such sum , mean arithmetic on left hand side of formula, this:

aggregate(a+b ~ c, data=d, fun=c(sum, mean))

i expect 3 column result c, mean(a+b) , sum(a+b).

i've looked @ summaryby 'doby' bundle fails arithmetic.

the closest i've found create custom function taking param , applying 2 aggregation functions within it, however, result still bit messy work there 2 columns, sec containing list both aggregations.

aggregate(a+b ~ c, info = d, fun=function(x) c(s=sum(x), m=mean(x)))

it's tedious, verbose , more computationally expensive doing 2 aggregations across same info , merging aggregations.

like this?

set.seed(1) d <- data.frame(a=rpois(100,1),b=rpois(100,1),c=rep(1:10,each=10)) result <- aggregate(a+b ~ c, info = d, fun=function(x) c(s=sum(x), m=mean(x))) result <- data.frame(result[,1],result[,2])

there indeed step 3-column info frame, not require multiple aggregation or merge.

incidentally, problem point out, regarding way aggregate(...) deals functions homecoming vectors, applies not formulas look on lhs.

result <- aggregate(a ~ c, info = d, fun=function(x) c(s=sum(x), m=mean(x)))

returns 2 column info frame, each element in sec column contains vector of length 2, if display result led believe result has 3 columns

head(result) # c a.s a.m # 1 1 11.0 1.1 # 2 2 13.0 1.3 # 3 3 8.0 0.8 # 4 4 10.0 1.0 # 5 5 12.0 1.2 # 6 6 7.0 0.7 str(result) # 'data.frame': 10 obs. of 2 variables: # $ c: int 1 2 3 4 5 6 7 8 9 10 # $ a: num [1:10, 1:2] 11 13 8 10 12 7 9 13 6 12 ... # ..- attr(*, "dimnames")=list of 2 # .. ..$ : null # .. ..$ : chr "s" "m"

r aggregate

No comments:

Post a Comment