- 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 number of zeros in each column of an R data frame?
To find the number of zeros in each column of an R data frame, we can follow the below steps −
First of all, create a data frame.
Then, use colSums function to find the number of zeros in each column.
Example 1
Create the data frame
Let’s create a data frame as shown below −
x<-rpois(30,1) y<-rpois(30,1) z<-rpois(30,1) df<-data.frame(x,y,z) df
Output
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
x y z 1 1 0 3 2 2 2 1 3 1 1 0 4 0 2 1 5 2 1 1 6 0 2 1 7 2 1 1 8 0 2 0 9 2 3 0 10 0 1 1 11 0 1 1 12 0 2 1 13 2 2 2 14 2 1 0 15 0 0 0 16 0 1 1 17 1 1 0 18 2 1 3 19 2 0 2 20 1 0 3 21 2 2 3 22 2 1 0 23 2 0 0 24 0 0 0 25 1 3 0 26 2 2 1 27 0 4 1 28 0 3 1 29 3 1 0 30 2 0 1
Find the number of zeros
Using colSums function to find the number of zeros in each column of data frame df −
x<-rpois(30,1) y<-rpois(30,1) z<-rpois(30,1) df<-data.frame(x,y,z) colSums(df==0)
Output
x y z 12 12 9
Example 2
Create the data frame
Let’s create a data frame as shown below −
v1<-sample(0:5,25,replace=TRUE) v2<-sample(0:5,25,replace=TRUE) v3<-sample(0:5,25,replace=TRUE) v4<-sample(0:5,25,replace=TRUE) dat<-data.frame(v1,v2,v3,v4) dat
Output
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
v1 v2 v3 v4 1 2 1 3 4 2 2 0 0 4 3 2 2 0 5 4 4 3 1 0 5 5 1 5 4 6 5 1 1 0 7 0 3 1 5 8 1 4 0 2 9 1 0 0 2 10 5 1 2 1 11 4 1 0 1 12 1 0 5 3 13 3 2 2 0 14 2 1 2 5 15 3 1 3 1 16 3 4 4 2 17 0 5 3 1 18 2 1 3 3 19 3 5 2 2 20 3 4 2 1 21 3 3 2 0 22 1 5 4 5 23 4 1 0 1 24 5 4 5 3 25 1 2 2 2
Find the number of zeros
Using colSums function to find the number of zeros in each column of data frame dat −
v1<-sample(0:5,25,replace=TRUE) v2<-sample(0:5,25,replace=TRUE) v3<-sample(0:5,25,replace=TRUE) v4<-sample(0:5,25,replace=TRUE) dat<-data.frame(v1,v2,v3,v4) colSums(dat==0)
Output
v1 v2 v3 v4 2 3 6 4
- Related Articles
- How to find the number of zeros in each row of an R data frame?
- How to find the percentage of zeros in each column of a data frame in R?
- How to find the number of NA’s in each column of an R data frame?
- How to find the percent of zeros in each row of an R data frame?
- How to find the percentage of each category in an R data frame column?
- How to find the count of each category in an R data frame column?
- How to find the percentage of missing values in each column of an R data frame?
- How to find the number of non-empty values in an R data frame column?
- How to find the row-wise frequency of zeros in an R data frame?
- How to find the number of zeros in each column of a matrix in R?
- How to find the index of the nearest smallest number in an R data frame column?
- How to find the number of unique values in each row of an R data frame?
- How to find the number of zeros in each column of a data.table object in R?
- How to find the column number of minimum values in each row for a data frame in R?
- Find the frequency of unique values for each column in an R data frame.

Advertisements