通常,累积总和是针对单个变量计算的,在某些情况下,是基于单个类别变量的,在极少数情况下,我们想要对两个类别变量进行计算。如果要为两个类别变量找到它,则需要将数据帧转换为data.table对象,并使用cumsum函数定义具有累加和的列。
请看以下数据帧:
> set.seed(1361) > Factor1<-as.factor(sample(LETTERS[1:4],20,replace=TRUE)) > Factor2<-as.factor(sample(c("T1","T2","T3","T4"),20,replace=TRUE)) > Response<-rpois(20,5) > df1<-data.frame(Factor1,Factor2,Response) > df1
输出结果
Factor1 Factor2 Response 1 A T2 9 2 B T1 8 3 B T1 2 4 A T2 3 5 B T1 7 6 B T2 7 7 D T2 7 8 D T4 7 9 C T4 6 10 B T1 6 11 A T2 4 12 A T2 4 13 C T1 7 14 B T3 1 15 A T3 6 16 D T1 3 17 B T1 8 18 D T4 5 19 D T2 3 20 C T1 4
加载data.table包:
> library(data.table)
将数据帧df1转换为data.table对象:
> dt1<-data.table(df1)
基于因子1和因子2创建具有累积总和的列CumulativeSums:
> dt1[,CumulativeSums:=cumsum(Response),by=list(Factor1,Factor2)] > dt1
输出结果
Factor1 Factor2 Response CumulativeSums 1: A T2 9 9 2: B T1 8 8 3: B T1 2 10 4: A T2 3 12 5: B T1 7 17 6: B T2 7 7 7: D T2 7 7 8: D T4 7 7 9: C T4 6 6 10: B T1 6 23 11: A T2 4 16 12: A T2 4 20 13: C T1 7 7 14: B T3 1 1 15: A T3 6 6 16: D T1 3 3 17: B T1 8 31 18: D T4 5 12 19: D T2 3 10 20: C T1 4 11
让我们看另一个例子:
> G1<-as.factor(sample(c("Hot","Cold"),20,replace=TRUE)) > G2<-as.factor(sample(c("Low","Medium","Large"),20,replace=TRUE)) > Y<-sample(1:100,20) > df2<-data.frame(G1,G2,Y) > df2
输出结果
G1 G2 Y 1 Hot Medium 60 2 Cold Low 94 3 Hot Low 22 4 Cold Medium 90 5 Hot Medium 16 6 Hot Large 32 7 Cold Low 44 8 Hot Low 73 9 Hot Medium 99 10 Hot Medium 68 11 Cold Medium 41 12 Cold Large 77 13 Cold Large 48 14 Cold Medium 20 15 Cold Medium 18 16 Cold Low 12 17 Cold Low 30 18 Hot Low 23 19 Cold Medium 26 20 Cold Medium 4
> dt2<-data.table(df2) > dt2[,CumulativeSums:=cumsum(Y),by=list(G1,G2)] > dt2
输出结果
G1 G2 Y CumulativeSums 1: Hot Medium 60 60 2: Cold Low 94 94 3: Hot Low 22 22 4: Cold Medium 90 90 5: Hot Medium 16 76 6: Hot Large 32 32 7: Cold Low 44 138 8: Hot Low 73 95 9: Hot Medium 99 175 10: Hot Medium 68 243 11: Cold Medium 41 131 12: Cold Large 77 77 13: Cold Large 48 125 14: Cold Medium 20 151 15: Cold Medium 18 169 16: Cold Low 12 150 17: Cold Low 30 180 18: Hot Low 23 118 19: Cold Medium 26 195 20: Cold Medium 4 199