How to create table of two factor columns in an R data frame?

R ProgrammingServer Side ProgrammingProgramming

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
raja
Updated on 03-Nov-2021 07:03:46

Advertisements