为 R 数据框中的三个分类列创建交叉表。
要为三个分类列创建交叉表,我们可以使用 xtabs 函数。xtabs 函数将为两列中的每个类别创建列联表,并将为第三列中的类别创建列联表。
查看以下示例以了解如何完成。
示例 1
以下代码段创建了一个示例数据框 -
df1<-data.frame(Status=c("Sold", "Available", "Available", "Sold", "Sold","In Queue", "In Queue", "Available", "Sold", "In Queue"), Gender = c("Female",
"Male", "Male", "Female", "Female", "Female", "Male", "Female", "Female",
"Female"), Confirm=c("No", "Yes", "No", "Yes", "No", "Yes", "Yes", "Yes",
"Yes", "No"))
df1
创建了以下数据框
Status Gender Confirm1 Sold Female No
2 Available Male Yes
3 Available Male No
4 Sold Female Yes
5 Sold Female No
6 In Queue Female Yes
7 In Queue Male Yes
8 Available Female Yes
9 Sold Female Yes
10 In Queue Female No
要在上面创建的数据框中为 df1 中的数据创建交叉表,请将以下代码添加到上面的代码段中 -
df1<-data.frame(Status=c("Sold", "Available", "Available", "Sold", "Sold",输出结果"In Queue", "In Queue", "Available", "Sold", "In Queue"), Gender = c("Female",
"Male", "Male", "Female", "Female", "Female", "Male", "Female", "Female",
"Female"), Confirm=c("No", "Yes", "No", "Yes", "No", "Yes", "Yes", "Yes",
"Yes", "No"))
xtabs(~Confirm+Gender+Status,data=df1)
, , Status = Available
如果您将上述所有给定的片段作为单个程序执行,它会生成以下输出 -
GenderConfirm Female Male
No 0 1
Yes 1 1
要在上面创建的数据框中为 df1 中的数据创建交叉表,请将以下代码添加到上面的代码段中 -
df1<-data.frame(Status=c("Sold", "Available", "Available", "Sold", "Sold",输出结果"In Queue", "In Queue", "Available", "Sold", "In Queue"), Gender = c("Female",
"Male", "Male", "Female", "Female", "Female", "Male", "Female", "Female",
"Female"), Confirm=c("No", "Yes", "No", "Yes", "No", "Yes", "Yes", "Yes",
"Yes", "No"))
xtabs(~Confirm+Gender+Status,data=df1)
, , Status = In Queue
如果您将上述所有给定的片段作为单个程序执行,它会生成以下输出 -
GenderConfirm Female Male
No 1 0
Yes 1 1
要在上面创建的数据框中为 df1 中的数据创建交叉表,请将以下代码添加到上面的代码段中 -
df1<-data.frame(Status=c("Sold", "Available", "Available", "Sold", "Sold",输出结果"In Queue", "In Queue", "Available", "Sold", "In Queue"), Gender = c("Female",
"Male", "Male", "Female", "Female", "Female", "Male", "Female", "Female",
"Female"), Confirm=c("No", "Yes", "No", "Yes", "No", "Yes", "Yes", "Yes",
"Yes", "No"))
xtabs(~Confirm+Gender+Status,data=df1)
, , Status = Sold
如果您将上述所有给定的片段作为单个程序执行,它会生成以下输出 -
GenderConfirm Female Male
No 2 0
Yes 2 0
示例 2
以下代码段创建了一个示例数据框 -
df2<-data.frame(Class=sample(c("I","II","III","IV"),20,replace=TRUE),Group=sample(c(
"G1","G2","G3"),20,replace=TRUE),Rank=sample(1:5,20,replace=TRUE))
df2
创建了以下数据框
Class Group Rank1 I G2 3
2 III G2 2
3 IV G2 3
4 I G3 4
5 I G1 3
6 IV G1 1
7 IV G2 3
8 II G1 1
9 III G1 1
10 I G2 2
11 IV G1 1
12 I G2 1
13 IV G3 1
14 I G2 1
15 II G2 3
16 III G1 4
17 I G2 2
18 IV G2 4
19 I G2 1
20 I G1 1
要在上面创建的数据框中为 df2 中的数据创建交叉表,请将以下代码添加到上面的代码段中 -
df2<-输出结果data.frame(Class=sample(c("I","II","III","IV"),20,replace=TRUE),Group=sample(c(
"G1","G2","G3"),20,replace=TRUE),Rank=sample(1:5,20,replace=TRUE))
xtabs(~Rank+Group+Class,data=df2)
, , Class = I
如果您将上述所有给定的片段作为单个程序执行,它会生成以下输出 -
GroupRank G1 G2 G3
1 1 3 0
2 0 2 0
3 1 1 0
4 0 0 1
要在上面创建的数据框中为 df2 中的数据创建交叉表,请将以下代码添加到上面的代码段中 -
df2<-输出结果data.frame(Class=sample(c("I","II","III","IV"),20,replace=TRUE),Group=sample(c(
"G1","G2","G3"),20,replace=TRUE),Rank=sample(1:5,20,replace=TRUE))
xtabs(~Rank+Group+Class,data=df2)
, , Class = II
如果您将上述所有给定的片段作为单个程序执行,它会生成以下输出 -
GroupRank G1 G2 G3
1 1 0 0
2 0 0 0
3 0 1 0
4 0 0 0
要在上面创建的数据框中为 df2 中的数据创建交叉表,请将以下代码添加到上面的代码段中 -
df2<-输出结果data.frame(Class=sample(c("I","II","III","IV"),20,replace=TRUE),Group=sample(c(
"G1","G2","G3"),20,replace=TRUE),Rank=sample(1:5,20,replace=TRUE))
xtabs(~Rank+Group+Class,data=df2)
, , Class = III
如果您将上述所有给定的片段作为单个程序执行,它会生成以下输出 -
GroupRank G1 G2 G3
1 1 0 0
2 0 1 0
3 0 0 0
4 1 0 0
要在上面创建的数据框中为 df2 中的数据创建交叉表,请将以下代码添加到上面的代码段中 -
df2<-输出结果data.frame(Class=sample(c("I","II","III","IV"),20,replace=TRUE),Group=sample(c(
"G1","G2","G3"),20,replace=TRUE),Rank=sample(1:5,20,replace=TRUE))
xtabs(~Rank+Group+Class,data=df2)
, , Class = IV
如果您将上述所有给定的片段作为单个程序执行,它会生成以下输出 -
GroupRank G1 G2 G3
1 2 0 1
2 0 0 0
3 0 2 0
4 0 1 0
以上是 为 R 数据框中的三个分类列创建交叉表。 的全部内容, 来源链接: utcz.com/z/360841.html