- 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 NA’s in each column of an R data frame?
Sometimes the data frame is filled with too many missing values/ NA’s and each column of the data frame contains at least one NA. In this case, we might want to find out how many missing values exists in each of the columns. Therefore, we can use colSums function along with is.na in the following manner: colSums(is.na(df)) #here df refers to data frame name.
Consider the below data frame −
Example
set.seed(109) x1<-sample(c(0:1,NA),20,replace=TRUE) x2<-sample(c(rpois(5,2),NA),20,replace=TRUE)df1<-data.frame(x1,x2) df1
Output
x1 x2 1 0 1 2 1 NA 3 NA 0 4 NA 0 5 1 1 6 1 1 7 NA NA 8 NA NA 9 0 1 10 NA 1 11 1 1 12 0 1 13 NA 1 14 0 0 15 1 1 16 NA 0 17 1 1 18 1 NA 19 NA NA 20 0 0
Finding the number of NA’s in each column of the data frame df1 −
Example
colSums(is.na(df1))
Output
x1 x2 6 4
Let’s have a look at another example −
Example
y1<-sample(c(100,105,NA,115,120),20,replace=TRUE) y2<-sample(c(rnorm(3,1,0.04),NA),20,replace=TRUE) df2<-data.frame(y1,y2) df2
Output
y1 y2 1 NA NA 2 NA NA 3 105 NA 4 115 0.9910075 5 120 NA 6 120 0.9547570 7 105 0.9547570 8 105 1.0468139 9 120 0.9910075 10 115 0.9547570 11 115 0.9910075 12 100 0.9910075 13 NA 1.0468139 14 120 1.0468139 15 NA 1.0468139 16 115 NA 17 115 1.0468139 18 100 NA 19 120 0.9910075 20 120 0.9910075
Finding the number of NA’s in each column of the data frame df2 −
Example
colSums(is.na(df2))
Output
y1 y2 3 3
- Related Articles
- How to find the number of zeros in each column 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 fill the NA with last observation in the column of an R data frame?
- How to find the percentage of missing values in each column of an R data frame?
- How to find the number of zeros in each row of an R data frame?
- How to find the number of non-empty values in an R data frame column?
- How to set a level of a factor column in an R data frame to NA?
- How to find the number of unique values in each row of an R data frame?
- How to replace NA values in columns of an R data frame form the mean of that column?
- How to find the index of the nearest smallest number in an R data frame column?
- How to find the frequency of NA values per row in an R data frame?
- Find the frequency of unique values for each column in an R data frame.
- How to fill NA values with previous values in an R data frame column?
- How to find the column number of minimum values in each row for a data frame in R?

Advertisements