如何根据R的data.table对象中的多列查找列的行数总和?
要根据 Rdata.table对象中的多列查找列的行数总和,我们可以按照以下步骤操作:
首先,创建一个data.table对象。
然后,使用聚合函数根据多列查找一列的行数总和。
示例
创建data.table对象
让我们创建一个data.table对象,如下所示 -
library(data.table)输出结果F1<-sample(c("Male","Female"),25,replace=TRUE)
F2<-sample(c("Low","Medium","High"),25,replace=TRUE)
F3<-sample(c("I","II","III"),25,replace=TRUE)
Response<-sample(1:100,25)
DT<-data.table(F1,F2,F3,Response)
DT
执行时,上述脚本生成以下内容output(this output will vary on your system due to randomization)-
F1 F2 F3 Response1: Male Medium I 2
2: Female Low I 32
3: Female High II 43
4: Male Medium I 24
5: Male Medium III 88
6: Female High I 95
7: Male Low II 68
8: Female High I 74
9: Female High II 12
10: Male High III 42
11: Male High I 62
12: Male High III 67
13: Female High III 38
14: Male Low III 73
15: Female Low III 3
16: Male Medium III 98
17: Female Low I 100
18: Male Medium II 89
19: Female High II 81
20: Female Low I 53
21: Female Medium II 91
22: Female High II 63
23: Female High II 60
24: Male Medium II 72
25: Female Medium III 56
F1 F2 F3 Response
根据多列查找一列的行数总和
使用聚合函数根据列 F1、F2 和 F3 查找列 Response 的行总和,如下所示 -
library(data.table)输出结果F1<-sample(c("Male","Female"),25,replace=TRUE)
F2<-sample(c("Low","Medium","High"),25,replace=TRUE)
F3<-sample(c("I","II","III"),25,replace=TRUE)
Response<-sample(1:100,25)
DT<-data.table(F1,F2,F3,Response)
DT[,sum(Response),by=.(F1,F2,F3)]
F1 F2 F3 V11: Male Medium I 26
2: Female Low I 185
3: Female High II 259
4: Male Medium III 186
5: Female High I 169
6: Male Low II 68
7: Male High III 109
8: Male High I 62
9: Female High III 38
10: Male Low III 73
11: Female Low III 3
12: Male Medium II 161
13: Female Medium II 91
14: Female Medium III 56
以上是 如何根据R的data.table对象中的多列查找列的行数总和? 的全部内容, 来源链接: utcz.com/z/361015.html