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

Updated on: 09-Nov-2021

826 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements