 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP 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 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
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 −
Example
df1$Zeros<-rowSums(df1==0) df1
Output
x1 x2 x3 x4 x5 x6 Zeros 1 0 1 1 0 4 3 2 2 0 4 2 1 4 10 1 3 0 3 2 0 0 3 3 4 0 5 0 0 4 4 3 5 1 1 1 0 3 6 1 6 1 2 1 1 4 5 0 7 1 0 2 0 0 4 3 8 1 0 0 1 3 2 2 9 0 2 0 0 5 1 3 10 0 4 1 1 4 2 1 11 0 3 0 0 1 3 3 12 0 0 0 0 1 5 4 13 1 2 0 1 5 8 1 14 1 1 2 1 1 10 0 15 1 3 0 1 4 6 1 16 1 5 2 0 0 8 2 17 0 1 2 0 0 4 3 18 0 5 2 1 3 1 1 19 1 2 2 0 0 2 2 20 1 3 2 1 3 4 0
Let’s have a look at another example −
Example
y1<-sample(0:2,20,replace=TRUE) y2<-sample(0:1,20,replace=TRUE) y3<-sample(0:5,20,replace=TRUE) y4<-sample(0:4,20,replace=TRUE) y5<-sample(0:1,20,replace=TRUE) y6<-sample(0:5,20,replace=TRUE) y7<-sample(0:10,20,replace=TRUE) df2<-data.frame(y1,y2,y3,y4,y5,y6) df2
Output
y1 y2 y3 y4 y5 y6 1 2 1 1 1 0 1 2 0 1 3 1 0 3 3 0 1 5 0 1 1 4 2 1 5 2 1 4 5 0 0 5 0 0 0 6 0 0 1 0 1 4 7 1 0 0 4 1 2 8 0 1 3 1 0 1 9 1 0 4 2 0 1 10 0 0 2 3 0 5 11 2 1 3 2 0 5 12 1 1 5 4 0 2 13 2 0 2 1 0 2 14 2 0 2 4 1 0 15 0 1 3 3 0 1 16 0 1 4 0 1 0 17 1 0 5 2 1 4 18 1 1 3 1 1 0 19 1 1 4 4 1 2 20 2 0 1 1 0 3
Counting number of zeros in each row −
Example
df2$No_of_Zeros <-rowSums(df2==0) df2
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
Advertisements
 