- 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
How to create table of two factor columns in an R data frame?
To create table of two factor columns in an R data frame, we can use table function and with function.
For Example, if we have a data frame called df that contains two factor columns say F1 and F2 then we can create table of these two columns by using the below command −
with(df,table(F1,F2))
Example 1
Following snippet creates a sample data frame −
Group<-sample(c("G1","G2","G3"),20,replace=TRUE) Class<-sample(c("First","Second","Third"),20,replace=TRUE) df1<-data.frame(Group,Class) df1
The following dataframe is created
Group Class 1 G1 First 2 G2 First 3 G3 First 4 G2 First 5 G1 Second 6 G2 Second 7 G3 Second 8 G3 Third 9 G2 First 10 G1 Third 11 G1 Second 12 G3 Second 13 G1 Second 14 G1 Third 15 G2 Second 16 G1 Second 17 G3 Third 18 G1 Second 19 G3 First 20 G1 Third
To create table of column Group and Class on the above created data frame, add the following code to the above snippet −
Group<-sample(c("G1","G2","G3"),20,replace=TRUE) Class<-sample(c("First","Second","Third"),20,replace=TRUE) df1<-data.frame(Group,Class) with(df1,table(Group,Class))
Output
If you execute all the above given snippets as a single program, it generates the following Output −
Class Group First Second Third G1 1 5 3 G2 3 2 0 G3 2 2 2
Example 2
Following snippet creates a sample data frame −
Gender<-sample(c("Male","Female"),20,replace=TRUE) Sal_Group<-sample(c("<=10K","10K and <=20K","=20K"),20,replace=TRUE) df2<-data.frame(Gender,Sal_Group) df2
The following dataframe is created
Gender Sal_Group 1 Male =20K 2 Male <=10K 3 Female <=10K 4 Male 10K and <=20K 5 Male <=10K 6 Female <=10K 7 Female 10K and <=20K 8 Female 10K and <=20K 9 Female =20K 10 Male =20K 11 Female 10K and <=20K 12 Male <=10K 13 Male =20K 14 Male =20K 15 Female 10K and <=20K 16 Male 10K and <=20K 17 Female <=10K 18 Female <=10K 19 Male =20K 20 Female 10K and <=20K
To create table of column Gender and Sal_Group on the above created data frame, add the following code to the above snippet −
Gender<-sample(c("Male","Female"),20,replace=TRUE) Sal_Group<-sample(c("<=10K","10K and <=20K","=20K"),20,replace=TRUE) df2<-data.frame(Gender,Sal_Group) with(df2,table(Gender,Sal_Group))
Output
If you execute all the above given snippets as a single program, it generates the following Output −
Sal_Group Gender <=10K =20K 10K and <=20K Female 4 1 5 Male 3 5 2
Example 3
Following snippet creates a sample data frame −
Category<-sample(c("C1","C2","C3","C4"),20,replace=TRUE) Sample<-sample(1:4,20,replace=TRUE) df3<-data.frame(Category,Sample) df3
The following dataframe is created
Category Sample 1 C4 4 2 C2 3 3 C2 3 4 C1 3 5 C3 4 6 C1 4 7 C2 1 8 C1 1 9 C1 1 10 C1 2 11 C4 3 12 C1 3 13 C4 2 14 C1 3 15 C4 2 16 C3 4 17 C1 3 18 C2 4 19 C1 2 20 C1 2
To create table of column Category and Sample on the above created data frame, add the following code to the above snippet −
Category<-sample(c("C1","C2","C3","C4"),20,replace=TRUE) Sample<-sample(1:4,20,replace=TRUE) df3<-data.frame(Category,Sample) with(df3,table(Category,Sample))
Output
If you execute all the above given snippets as a single program, it generates the following Output −
Sample Category 1 2 3 4 C1 2 3 4 1 C2 1 0 2 1 C3 0 0 0 2 C4 0 2 1 1
- Related Articles
- How to subset factor columns in an R data frame?
- How to find the frequency table for factor columns in an R data frame?
- How to find the cumulative sums by using two factor columns in an R data frame?
- How to extract only factor columns name from an R data frame?
- How to convert a data frame into table for two factor columns and one numeric column in R?
- How to create histogram of all columns in an R data frame?
- How to create scatterplot for factor levels in an R data frame?
- How to create a column in an R data frame that contains the multiplication of two columns?
- Create a data frame with numerical as well as factor columns in R.
- How to find the two factor interaction variables in an R data frame?
- How to create a subset for a factor level in an R data frame?
- How to create scatterplot using data frame columns in R?
- How to collapse factor levels in an R data frame?
- How to standardize columns in an R data frame?
- How to compare two columns in an R data frame for an exact match?
