Tuesday, 15 February 2011

r - aggregate a column by sum and another column by mean at the same time -



r - aggregate a column by sum and another column by mean at the same time -

i want utilize aggregate function on date frame sum 1 column , take average of column.

here illustration info frame

manager category amount sqft joe rent 150 500 alice rent 250 700 joe utilities 50 500 alice utilities 75 700

i cannot below. there easy way ?

avg_cpsf=aggregate(cbind(amount,sqft)~manager,data=aaa,fun=c(sum,mean)

eventually need

manager amount sqft joe 200 500 alice 325 700

so can calculate cost per square foot doing amount/sqft

there several ways this. here (all assuming we're starting data.frame named "mydf"):

using ave , unique

unique(within(mydf, { amount <- ave(amount, manager, fun = sum) sqft <- ave(sqft, manager, fun = mean) rm(category) })) # manager amount sqft # 1 joe 200 500 # 2 alice 325 700

using data.table:

library(data.table) dt <- data.table(mydf) dt[, list(amount = sum(amount), sqft = mean(sqft)), = "manager"] # manager amount sqft # 1: joe 200 500 # 2: alice 325 700

using "sqldf":

library(sqldf) sqldf("select manager, sum(amount) `amount`, avg(sqft) `sqft` mydf grouping manager")

using aggregate , merge:

merge(aggregate(amount ~ manager, mydf, sum), aggregate(sqft ~ manager, mydf, mean))

r aggregate

No comments:

Post a Comment