在 R 中查找基于多个分类列的互斥组组合的频率。


为了在 R 数据框中查找互斥组组合的频率,我们可以使用 dplyr 包的 count 函数以及 ungroup 函数。

例如,如果我们有一个名为 df 的数据框,其中包含四个分组列,例如 Grp1、Grp2、Grp3 和 Grp4,那么我们可以使用以下命令计算 df 中唯一的组组合:

count(df,Grp1,Grp2,Grp3,Grp4)%%ungroup()

示例 1

以下代码段创建了一个示例数据框:

Open Compiler
Class1<-sample(c("First","Second","Third"),20,replace=TRUE) Class2<-sample(c("First","Second","Third"),20,replace=TRUE) Class3<-sample(c("First","Second","Third"),20,replace=TRUE) Score<-sample(1:50,20) df1<-data.frame(Class1,Class2,Class3,Score) df1

输出

如果将以上所有代码段作为一个程序执行,则会生成以下输出:

  Class1 Class2 Class3 Score
1    Third  First Third 40
2    First  First Second 38
3    First Second Third 25
4    First Second Second 2
5    First  Third Third 12
6    First Second First 13
7   Second  Third Third 31
8    First  First First 15
9    First  Third Third 43
10  Second  First Second 28
11   First First Third 22
12   Third  Third First 50
13   First Second Second 39
14   First  First First 41
15  Second  Third Third 49
16  Second  First First 36
17   Third  Third First 20
18  Second Second Second 19
19   First  Third First 5
20  Second  First Third 47

要加载 dplyr 包并查找上面创建的数据框中 Class1、Class2 和 Class3 组的互斥组组合的频率,请将以下代码添加到以上代码段中:

Open Compiler
Class1<-sample(c("First","Second","Third"),20,replace=TRUE) Class2<-sample(c("First","Second","Third"),20,replace=TRUE) Class3<-sample(c("First","Second","Third"),20,replace=TRUE) Score<-sample(1:50,20) df1<-data.frame(Class1,Class2,Class3,Score) library(dplyr) count(df1,Class1,Class2,Class3)%%ungroup()

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

输出

如果将以上所有代码段作为一个程序执行,则会生成以下输出:

   Class1 Class2 Class3 n
1   First  First First 2
2   First  First Second 1
3   First  First Third 1
4   First Second First 1
5   First Second Second 2
6   First Second Third 1
7   First  Third First 1
8   First  Third Third 2
9  Second  First First 1
10 Second  First Second 1
11 Second  First Third 1
12 Second Second Second 1
13 Second  Third Third 2
14  Third  First Third 1
15  Third  Third First 2

示例 2

以下代码段创建了一个示例数据框:

Open Compiler
Grp1<-sample(1:2,20,replace=TRUE) Grp2<-sample(1:2,20,replace=TRUE) Grp3<-sample(1:2,20,replace=TRUE) df2<-data.frame(Grp1,Grp2,Grp3) df2

创建以下数据框

  Grp1 Grp2 Grp3
1  2     1   1  
2  1     1   1
3  1     2   1
4  2     1   1
5  2     2   2
6  1     2   2
7  2     1   2
8  1     1   2
9  2     2   1
10 1     2   2
11 2     2   2
12 1     1   1
13 2     1   1
14 1     1   2
15 2     2   2
16 1     1   2
17 2     2   2
18 1     2   2
19 2     1   1
20 2     2   2

要查找上面创建的数据框中 Grp1、Grp2 和 Grp3 组的互斥组组合的频率,请将以下代码添加到以上代码段中:

Grp1<-sample(1:2,20,replace=TRUE) Grp2<-sample(1:2,20,replace=TRUE) Grp3<-sample(1:2,20,replace=TRUE) df2<-data.frame(Grp1,Grp2,Grp3) count(df2,Grp1,Grp2,Grp3)%%ungroup()

输出

如果将以上所有代码段作为一个程序执行,则会生成以下输出:

   Grp1 Grp2 Grp3 n
1   1    1     1  2
2   1    1     2  3
3   1    2     1  1
4   1    2     2  3
5   2    1     1  4
6   2    1     2  1
7   2    2     1  1
8   2    2     2  5

更新于: 2021年11月2日

460 次查看

开启您的 职业生涯

通过完成课程获得认证

开始学习
广告