如何在R中的data.table对象列中找到每个类别的计数?
要查找data.tableR 中对象列中每个类别的计数,我们可以按照以下步骤操作 -
首先,创建一个data.table对象。
然后,与n一起分组后使用dplyr包的汇总功能。
示例
创建data.table对象
让我们创建一个data.table对象,如下所示 -
library(data.table)输出结果factor<-sample(c("very low","low","medium","high","very high"),25,replace=TRUE)
response<-rnorm(25)
DT<-data.table(factor,response)
DT
执行时,上述脚本生成以下内容output(this output will vary on your system due to randomization)-
factor response1: very high -0.14064701
2: high -0.36029663
3: low -0.50825291
4: very high -2.07959316
5: high 0.25578922
6: very high 1.17946264
7: low 1.32803179
8: medium -0.23736325
9: medium -0.07440274
10: very low 0.32669673
11: very low 0.54953485
12: high -0.93197732
13: very high -0.77787846
14: very low 0.63808636
15: very high 0.76008767
16: very high -0.38712279
17: very high -0.64259955
18: high 0.30249791
19: high 0.16984229
20: very low -0.24732679
21: high -0.47456705
22: low 0.08789350
23: low 0.32541943
24: medium -0.27170782
25: high -0.09860207
factor response
查找data.table对象中每个类别的计数
在与 n 一起分组后使用 dplyr 包的汇总函数来查找data.table对象 DT 的因子列中每个类别的计数-
library(data.table)输出结果factor<-sample(c("very low","low","medium","high","very high"),25,replace=TRUE)
response<-rnorm(25)
DT<-data.table(factor,response)
library(dplyr)
DT %>% group_by(factor) %>% summarise(count=n())
# A tibble: 5 x 2factor count
<chr> <int>
1 high 7
2 low 4
3 medium 3
4 very high 7
5 very low 4
以上是 如何在R中的data.table对象列中找到每个类别的计数? 的全部内容, 来源链接: utcz.com/z/356350.html