- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
- Related Articles
- How to remove rows for categorical columns that has three or less combination of duplicates in an R data frame?
- How to create a frequency column for categorical variable in an R data frame?
- How to test for significant relationship between two categorical columns of an R data frame?
- How to standardize only numerical columns in an R data frame if categorical columns also exist?
- How to standardize columns if some columns are categorical in R data frame?
- How to find the counts of categories in categorical columns in an R data frame?
- How to convert a data frame with categorical columns to numeric in R?
- How to round each value in columns if some columns are categorical in R data frame?
- How to create histogram of all columns in an R data frame?
- How to find the range of columns if some columns are categorical in R data frame?
- Find the percentiles for multiple columns in an R data frame.
- How to create a new data frame for the mean of rows of some columns from an R data frame?
- How to create table of two factor columns in an R data frame?
- How to find the cumulative sum of columns if some columns are categorical in R data frame?
- How to create scatterplot using data frame columns in R?
