Create cross tabulation for three categorical columns in an R data frame.


To create cross tabulation for three categorical columns, we can use xtabs function. The xtabs function will create contingency table for each category in two columns and each contingency table will be created for the category in the third column.

Check out the below Examples to understand how it can be done.

Example 1

Following snippet creates a sample data frame −

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

The following dataframe is created

 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

To create cross tabulation for data in df1 on the above created data frame, add the following code to the above snippet −

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

Output

If you execute all the above given snippets as a single program, it generates the following Output −

    Gender
Confirm Female Male
 No       0     1
Yes       1     1

To create cross tabulation for data in df1 on the above created data frame, add the following code to the above snippet −

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

Output

If you execute all the above given snippets as a single program, it generates the following Output −

   Gender
Confirm Female Male
 No       1    0
 Yes      1    1

To create cross tabulation for data in df1 on the above created data frame, add the following code to the above snippet −

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

Output

If you execute all the above given snippets as a single program, it generates the following Output −

Gender
Confirm Female Male
   No     2    0
   Yes    2    0

Example 2

Following snippet creates a sample data frame −

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

The following dataframe is created

  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

To create cross tabulation for data in df2 on the above created data frame, add the following code to the above snippet −

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

Output

If you execute all the above given snippets as a single program, it generates the following Output −

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

To create cross tabulation for data in df2 on the above created data frame, add the following code to the above snippet −

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

Output

If you execute all the above given snippets as a single program, it generates the following Output −

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

To create cross tabulation for data in df2 on the above created data frame, add the following code to the above snippet −

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

Output

If you execute all the above given snippets as a single program, it generates the following Output −

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

To create cross tabulation for data in df2 on the above created data frame, add the following code to the above snippet −

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

Output

If you execute all the above given snippets as a single program, it generates the following Output −

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

Updated on: 02-Nov-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements