分组求和
刚遇到一个问题,就是分组求和在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 consumption
1 a 1
2 b 2
3 a 3
4 m 4
5 a 5
6 b 6
tapply(a$consumption,a$customer,sum)
a b m
9 8 4
tapply(a$consumption,a$customer,length)
a b m
3 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 x
1 a A 9
2 b B 8
3 m B 4
library(data.table)
consume<-data.table(consume)
setkey(consume,customer)
a1<-consume[,sum(consumption),customer]
posted on 2016-03-23 14:56 MartinChau 阅读(311) 评论(0) 收藏 举报
浙公网安备 33010602011771号