刚遇到一个问题,就是分组求和在R里怎么实现比较简便?
对应SQL语句为:SELECT customer, sum(consumption) FROM consume group by customer然后就是类似的,分组计数怎么做?
a=data.frame(customer=c('a','b','a','m','a','b'),consumption=1:6)a customer consumption1 a 12 b 23 a 34 m 45 a 56 b 6tapply(a$consumption,a$customer,sum)a b m9 8 4tapply(a$consumption,a$customer,length)a b m3 2 1
在搞复杂点。
a=data.frame(customer=c('a','b','a','m','a','b'),consumption=1:6,groups=c('A','B','A','B','A','B'))aggregate(a$consumption, list(a$customer,a$groups), sum) Group.1 Group.2 x1 a A 92 b B 83 m B 4library(data.table)consume<-data.table(consume)setkey(consume,customer)a1<-consume[,sum(consumption),customer]