- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- 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 find the frequency of a particular string in a column based on another column in an R data frame using dplyr package?

When we have two or more categorical columns in an R data frame with strings as level of the categories or numbers as strings/integers then we can find the frequency of one based on another. This will help us to identify the cross-column frequencies and we can understand the distribution of one categorical based on another column. To do this with dplyr package, we can use filter function.

## Example

Consider the below data frame −

Group<−sample(1:5,20,replace=TRUE) Standard<−sample(c("I","II","III"),20,replace=TRUE) df1<−data.frame(Group,Standard) df1

## Output

Group Standard 1 3 III 2 5 III 3 5 I 4 3 I 5 2 II 6 4 II 7 3 III 8 2 I 9 1 II 10 4 III 11 3 II 12 4 III 13 4 III 14 4 III 15 4 III 16 4 III 17 5 III 18 3 II 19 5 III 20 1 III

Finding the frequency of Groups for Standards −

library(dplyr) df1%>%filter(Standard=="I")%>%count(Group)

## Output

Group n 1 2 1 2 3 1 3 5 1

## Example

df1%>%filter(Standard=="II")%>%count(Group)

## Output

Group n 1 1 1 2 2 1 3 3 2 4 4 1

## Example

df1%>%filter(Standard=="III")%>%count(Group)

## Output

Group n 1 1 1 2 3 2 3 4 6 4 5 3

## Let’s have a look at another example −

Class<−sample(c("First","Second","Third"),20,replace=TRUE) Gender<−sample(c("Male","Female"),20,replace=TRUE) df2<−data.frame(Gender,Class) df2

## Output

Gender Class 1 Female Third 2 Female First 3 Female Second 4 Male Third 5 Male Third 6 Female Second 7 Male First 8 Female Third 9 Female Second 10 Female Second 11 Female First 12 Female Second 13 Male First 14 Female Third 15 Female Third 16 Male Third 17 Male Third 18 Male Second 19 Female Second 20 Male Second df2%>%filter(Class=="Third")%>%count(Gender) Gender n 1 Female 4 2 Male 4 df2%>%filter(Class=="First")%>%count(Gender) Gender n 1 Female 2 2 Male 2 df2%>%filter(Class=="Second")%>%count(Gender) Gender n 1 Female 6 2 Male 2

- Related Questions & Answers
- How to assign a column value in a data frame based on another column in another R data frame?
- How to find the column number of a string column based on a string match in an R data frame?
- How to find the column means of a column based on another column values that represent factor in an R data frame?
- How to find the position of a data frame’s column value based on a column value of another data frame in R?
- How to create a new column in an R data frame based on some condition of another column?
- How to extract a data frame’s column value based on a column value of another data frame in R?
- How to extract a particular value based on index from an R data frame column?
- How to subset rows of an R data frame based on duplicate values in a particular column?
- How to find the average of a particular column in R data frame?
- How to remove rows containing missing value based on a particular column in an R data frame?
- Find the sum of a column values based on another numerical column in R.
- How to extract columns of a data frame in R using dplyr package?
- How to find the frequency of continuously repeated string values in an R data frame column?
- How to remove rows from data frame in R based on grouping value of a particular column?
- How to subset a data frame by excluding a column using dplyr in R?

Advertisements