# How to find the row-wise frequency of zeros in an R data frame?

In data analysis, we need to be very cautious about repeated values because they might be inputted purposely to create bias in the data and this value could be a zero as well. It happens in situations when we have missing data and the data collector replaces missing values with zeros which is a wrong practice. To find the row-wise frequency of zeros in an R data frame, we can use rowSums function for zero values by using the syntax −

rowSums(“data_frame_name”==0)

Consider the below data frame −

## Example

Live Demo

set.seed(189)
x1<-sample(0:1,20,replace=TRUE)
x2<-sample(0:5,20,replace=TRUE)
x3<-sample(0:2,20,replace=TRUE)
x4<-sample(0:1,20,replace=TRUE)
x5<-sample(0:5,20,replace=TRUE)
x6<-sample(0:10,20,replace=TRUE)
df1<-data.frame(x1,x2,x3,x4,x5,x6) df1

## Output

x1 x2  x3 x4 x5 x6
1  1  5  2   1  3  1
2  1  3  0   0  5  2
3  1  2  2  1  2   5
4  1  4  0  0  0   7
5  0  0  2  0  5   0
6  0  5  1  1  2   5
7  0  5  1  1  1   6
8  0  4  1  0  3   1
9  0  4  1  1  3   4
10 0  1  2  0  4   5
11 1 1  2  0   4   0
12 1  2  0  0  5   1
13 1  3  0  0  1   5
14 1  0  0  0  5   1
15 0 3  0  1  5   10
16 0 3  2  1  3    2
17 1 3  0  1  1    7
18 0 3  1  1  2    9
19 1 3  2  0 3     5
20 1 4  0  1 2     1

Counting number of zeros in each row −

## Output

y1 y2 y3 y4 y5 y6    No_of_Zeros
1  1  0  2   0  1 5 2
2  1  1  0   3 1 2  1
3  1  1 3    4 1 5 0
4  0  1  0   1 0 0 4
5  1  0  2   1 1 2 1
6  1  1  0   4 1 0 2
7  2  1  0   2 0 3 2
8  1  1  3   2 0 1 1
9  2  0  2   0 0 4 3
10 2  1  1   2 0 5 1
11 0  0  1   4 1 3 2
12 2  1  4   3 0 1 1
13 1  1  3   3 0 1 1
14 1  0  3   2 0 0 3
15 0  1  1   0 0 2 3
16 1  0  5   0 1 5 2
17 0  1 0    3 1 5 2
18 2  1 0    3 1 0 2
19 2  0 0    0 0 5 4
20 1  1 5    4 1 4 0