How to find the number of NA’s in each column of an R data frame?

R ProgrammingServer Side ProgrammingProgramming

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

 Live Demo

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

 Live Demo

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
raja
Updated on 09-Oct-2020 14:19:24

Advertisements