- 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 find the frequency of particular value in rows of an R data frame?
To find the frequency of particular value in rows of an R data frame, we can use mutate function of dplyr package along with rowSums function.
For example, if we have a data frame called df then we can find the number of 5’s in each row of df by using the below command −
df%>%mutate(Number_of_Fives=rowSums(.==1))
Example 1
Following snippet creates a sample data frame −
x1<-rpois(20,1) x2<-rpois(20,1) x3<-rpois(20,1) x4<-rpois(20,1) x5<-rpois(20,1) df1<-data.frame(x1,x2,x3,x4,x5) df1
The following dataframe is created −
x1 x2 x3 x4 x5 1 1 0 0 1 1 2 0 0 0 1 2 3 1 2 4 0 0 4 1 2 1 0 3 5 3 3 1 2 2 6 2 2 2 1 3 7 1 0 0 2 0 8 4 3 3 3 0 9 1 1 1 0 0 10 2 1 1 2 1 11 1 2 0 1 0 12 1 2 3 1 1 13 1 1 4 0 2 14 0 1 1 3 2 15 1 0 1 0 1 16 1 3 0 1 2 17 1 0 2 1 1 18 2 0 0 0 1 19 0 1 0 0 0 20 0 0 0 0 0
To load dplyr package and find the number of ones in each row of df1, add the following code to the above snippet −
library(dplyr) df1%>%mutate(Number_of_Ones=rowSums(.==1))
Output
If you execute all the above given snippets as a single program, it generates the following output −
x1 x2 x3 x4 x5 Number_of_Ones 1 1 0 0 1 1 3 2 0 0 0 1 2 1 3 1 2 4 0 0 1 4 1 2 1 0 3 2 5 3 3 1 2 2 1 6 2 2 2 1 3 1 7 1 0 0 2 0 1 8 4 3 3 3 0 0 9 1 1 1 0 0 3 10 2 1 1 2 1 3 11 1 2 0 1 0 2 12 1 2 3 1 1 3 13 1 1 4 0 2 2 14 0 1 1 3 2 2 15 1 0 1 0 1 3 16 1 3 0 1 2 2 17 1 0 2 1 1 3 18 2 0 0 0 1 1 19 0 1 0 0 0 1 20 0 0 0 0 0 0
Example 2
Following snippet creates a sample data frame −
y1<-sample(1:3,20,replace=TRUE) y2<-sample(1:3,20,replace=TRUE) y3<-sample(1:3,20,replace=TRUE) y4<-sample(1:3,20,replace=TRUE) df2<-data.frame(y1,y2,y3,y4) df2
The following dataframe is created −
y1 y2 y3 y4 1 2 1 2 1 2 2 3 1 1 3 3 1 2 2 4 3 1 1 1 5 3 1 3 2 6 2 3 2 2 7 1 3 1 1 8 1 2 1 2 9 1 2 3 3 10 2 3 3 2 11 3 1 1 2 12 3 2 1 1 13 2 2 2 3 14 2 1 2 2 15 1 2 1 2 16 3 3 2 3 17 1 2 3 3 18 3 3 3 3 19 2 1 2 2 20 1 1 2 1
To find the number of ones in each row of df2, add the following code to the above snippet −
df2%>%mutate(Number_of_Ones=rowSums(.==1))
Output
If you execute all the above given snippets as a single program, it generates the following output −
y1 y2 y3 y4 Number_of_Ones 1 2 1 2 1 2 2 2 3 1 1 2 3 3 1 2 2 1 4 3 1 1 1 3 5 3 1 3 2 1 6 2 3 2 2 0 7 1 3 1 1 3 8 1 2 1 2 2 9 1 2 3 3 1 10 2 3 3 2 0 11 3 1 1 2 2 12 3 2 1 1 2 13 2 2 2 3 0 14 2 1 2 2 1 15 1 2 1 2 2 16 3 3 2 3 0 17 1 2 3 3 1 18 3 3 3 3 0 19 2 1 2 2 1 20 1 1 2 1 3
- Related Articles
- How to find the frequency of each value in an R data frame?
- How to filter rows by excluding a particular value in columns of the R data frame?
- How to remove rows containing missing value based on a particular column in an R data frame?
- How to remove rows from data frame in R based on grouping value of a particular column?
- How to find the unique rows in an R data frame?
- How to find the row-wise frequency of zeros in an R data frame?
- How to find the correlation matrix for rows of an R data frame?
- How to find the ID wise frequency in an R data frame?
- How to subset rows of an R data frame based on duplicate values in a particular column?
- How to remove rows from an R data frame based on frequency of values in grouping column?
- How to find the frequency of NA values per row in an R data frame?
- How to find the percent of NA’s in R data frame rows?
- How to find the average of a particular column in R data frame?
- How to create a random sample of some percentage of rows for a particular value of a column from an R data frame?
- How to delete rows in an R data frame?
