如何计算R中类别变量组合的行数?
当我们有两个分类变量时,每个变量可能为另一个变量具有不同的行数。这有助于我们理解这两个类别变量的组合值。我们可以使用dplyr包的count函数找到这种类型的行。
示例
考虑以R为基础的CO2数据
> head(CO2,20)> head(CO2,20)
Plant Type Treatment conc uptake
1 Qn1 Quebec nonchilled 95 16.0
2 Qn1 Quebec nonchilled 175 30.4
3 Qn1 Quebec nonchilled 250 34.8
4 Qn1 Quebec nonchilled 350 37.2
5 Qn1 Quebec nonchilled 500 35.3
6 Qn1 Quebec nonchilled 675 39.2
7 Qn1 Quebec nonchilled 1000 39.7
8 Qn2 Quebec nonchilled 95 13.6
9 Qn2 Quebec nonchilled 175 27.3
10 Qn2 Quebec nonchilled 250 37.1
11 Qn2 Quebec nonchilled 350 41.8
12 Qn2 Quebec nonchilled 500 40.6
13 Qn2 Quebec nonchilled 675 41.4
14 Qn2 Quebec nonchilled 1000 44.3
15 Qn3 Quebec nonchilled 95 16.2
16 Qn3 Quebec nonchilled 175 32.4
17 Qn3 Quebec nonchilled 250 40.3
18 Qn3 Quebec nonchilled 350 42.1
19 Qn3 Quebec nonchilled 500 42.9
20 Qn3 Quebec nonchilled 675 43.9
> tail(CO2,20)
Plant Type Treatment conc uptake
65 Mc1 Mississippi chilled 175 14.9
66 Mc1 Mississippi chilled 250 18.1
67 Mc1 Mississippi chilled 350 18.9
68 Mc1 Mississippi chilled 500 19.5
69 Mc1 Mississippi chilled 675 22.2
70 Mc1 Mississippi chilled 1000 21.9
71 Mc2 Mississippi chilled 95 7.7
72 Mc2 Mississippi chilled 175 11.4
73 Mc2 Mississippi chilled 250 12.3
74 Mc2 Mississippi chilled 350 13.0
75 Mc2 Mississippi chilled 500 12.5
76 Mc2 Mississippi chilled 675 13.7
77 Mc2 Mississippi chilled 1000 14.4
78 Mc3 Mississippi chilled 95 10.6
79 Mc3 Mississippi chilled 175 18.0
80 Mc3 Mississippi chilled 250 17.9
81 Mc3 Mississippi chilled 350 17.9
82 Mc3 Mississippi chilled 500 17.9
83 Mc3 Mississippi chilled 675 18.9
84 Mc3 Mississippi chilled 1000 19.9
> library(dplyr)
查找每个处理类型变量的行数-
> count(CO2, Type, Treatment)# A tibble: 4 x 3
Type Treatment n
<fct> <fct> <int>
1 Quebec nonchilled 21
2 Quebec chilled 21
3 Mississippi nonchilled 21
4 Mississippi chilled 21
为类型变量查找每个工厂的行数-
> count(CO2, Type, Plant)# A tibble: 12 x 3
Type Plant n
<fct> <ord> <int>
1 Quebec Qn1 7
2 Quebec Qn2 7
3 Quebec Qn3 7
4 Quebec Qc1 7
5 Quebec Qc3 7
6 Quebec Qc2 7
7 Mississippi Mn3 7
8 Mississippi Mn2 7
9 Mississippi Mn1 7
10 Mississippi Mc2 7
11 Mississippi Mc3 7
12 Mississippi Mc1 7
查找每个处理的工厂变量的行数-
> count(CO2, Plant, Treatment)# A tibble: 12 x 3
Plant Treatment n
<ord> <fct> <int>
1 Qn1 nonchilled 7
2 Qn2 nonchilled 7
3 Qn3 nonchilled 7
4 Qc1 chilled 7
5 Qc3 chilled 7
6 Qc2 chilled 7
7 Mn3 nonchilled 7
8 Mn2 nonchilled 7
9 Mn1 nonchilled 7
10 Mc2 chilled 7
11 Mc3 chilled 7
12 Mc1 chilled 7
以上是 如何计算R中类别变量组合的行数? 的全部内容, 来源链接: utcz.com/z/341141.html