- Related Questions & Answers
- How to find the counts of categories in categorical columns in an R Programming data frame?
- How to find the mean of a numerical column by two categorical columns in an R data frame?
- How to standardize only numerical columns in an R data frame if categorical columns also exist?
- How to test for significant relationship between two categorical columns of an R data frame?
- How to find the median of all columns in an R data frame?
- How to find the number of numerical columns in an R data frame?
- How to subset columns that has less than four categories in an R data frame?
- How to convert a data frame with categorical columns to numeric in R?
- How to find the sum based on a categorical variable in an R data frame?
- How to find the absolute distance between columns of an R data frame?
- How to reorder the columns in an R data frame?
- How to change the order of columns in an R data frame?
- How to standardize columns in an R data frame?
- How to find the minimum and maximum of columns values in an R data frame?
- How to find the mean of columns of an R data frame or a matrix?

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

If we have two categorical columns in an R data frame then we can find the frequency/count of each category with respect to each category in the other column. This will help us to compare the frequencies for all categories. To find the counts of categories, we can use table function as shown in the below examples.

Consider the below data frame −

x1<−sample(c("Child","Teen","Adult","Old"),20,replace=TRUE) x2<−sample(c("Unemployed","Employed"),20,replace=TRUE) df1<−data.frame(x1,x2) df1

x1 x2 1 Old Unemployed 2 Child Unemployed 3 Adult Employed 4 Adult Unemployed 5 Adult Employed 6 Teen Employed 7 Old Employed 8 Child Unemployed 9 Child Employed 10 Adult Unemployed 11 Child Unemployed 12 Old Employed 13 Child Unemployed 14 Child Employed 15 Teen Employed 16 Adult Employed 17 Adult Unemployed 18 Old Employed 19 Adult Unemployed 20 Child Employed

Finding the counts of categories in both columns of df1 −

table(df1$x1,df1$x2)

Employed Unemployed Adult 3 4 Child 3 4 Old 3 1 Teen 2 0

y1<−sample(c("Married","Unmarried"),20,replace=TRUE) y2<−sample(c("Satisfied","Not-Satisfied"),20,replace=TRUE) df2<−data.frame(y1,y2) df2

y1 y2 1 Married Not-Satisfied 2 Unmarried Not-Satisfied 3 Married Not-Satisfied 4 Unmarried Not-Satisfied 5 Married Satisfied 6 Married Not-Satisfied 7 Unmarried Satisfied 8 Married Satisfied 9 Unmarried Not-Satisfied 10 Unmarried Not-Satisfied 11 Unmarried Not-Satisfied 12 Unmarried Not-Satisfied 13 Married Satisfied 14 Married Satisfied 15 Married Satisfied 16 Married Not-Satisfied 17 Married Satisfied 18 Unmarried Satisfied 19 Married Satisfied 20 Married Satisfied

Finding the counts of categories in both columns of df2 −

table(df2$y1,df2$y2)

Not−Satisfied Satisfied Married 4 8 Unmarried 6 2

Advertisements