根据 R 中的多个分类列查找排他组组合的频率。
要查找R数据帧中独占组组合的频率,我们可以使用dplyr包的计数功能以及取消组功能。
例如,如果我们有一个名为df的数据框,其中包含四个分组列,比如Grp1、Grp2、Grp3和Grp4,那么我们可以使用以下命令计算df中的唯一组组合-
count(df,Grp1,Grp2,Grp3,Grp4)%%ungroup()
示例1
以下代码段创建了一个示例数据框-
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的独占组组合的频率,请将以下代码添加到上面的代码段中-
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()输出结果
如果您将上述所有给定的片段作为单个程序执行,它会生成以下输出-
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
以下代码段创建了一个示例数据框-
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