
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Find Frequency of a String in R Data Frame using dplyr
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
Advertisements