在 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 Confirm
1 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

输出

如果您将所有上面给出的代码片段作为单个程序执行,它将生成以下输出 -

    Gender
Confirm 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

输出

如果您将所有上面给出的代码片段作为单个程序执行,它将生成以下输出 -

   Gender
Confirm 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

输出

如果您将所有上面给出的代码片段作为单个程序执行,它将生成以下输出 -

Gender
Confirm 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 Rank
1 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

输出

如果您将所有上面给出的代码片段作为单个程序执行,它将生成以下输出 -

Group
Rank 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

输出

如果您将所有上面给出的代码片段作为单个程序执行,它将生成以下输出 -

Group
Rank 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

输出

如果您将所有上面给出的代码片段作为单个程序执行,它将生成以下输出 -

Group
Rank 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

输出

如果您将所有上面给出的代码片段作为单个程序执行,它将生成以下输出 -

    Group
Rank G1 G2 G3
1    2   0  1
2    0   0  0
3    0   2  0
4    0   1  0

更新于: 2021年11月2日

1K+ 次查看

启动你的 职业生涯

通过完成课程获得认证

开始
广告