# How to check which value is NA in an R data frame?

To check which value in NA in an R data frame, we can use apply function along with is.na function.

For Example, if we have a data frame called df that contains some NA values then we can check which value is NA by using the command mentioned below −

apply(df,2, function(x) is.na(x))

This will return the data frame in logical form with TRUE and FALSE. Check out the below Examples to understand how it works.

## Example 1

Following snippet creates a sample data frame −

x1<-sample(c(NA,round(rnorm(3),2)),20,replace=TRUE)
x2<-sample(c(NA,round(rnorm(3,10,2),2)),20,replace=TRUE)
x3<-sample(c(NA,round(rnorm(3,25,3.2),2)),20,replace=TRUE)
df1<-data.frame(x1,x2,x3)
df1

The following dataframe is created

x1    x2 x3
1  -0.02  7.82 31.24
2  -0.50  9.21 31.24
3     NA    NA 21.65
4  -0.02  7.82    NA
5     NA    NA 31.24
6     NA  7.82 29.91
7  -0.02  7.82 29.91
8     NA    NA 29.91
9  -0.02  7.82 31.24
10  1.51    NA    NA
11 -0.02  9.21 29.91
12    NA    NA 31.24
13 -0.50  9.21 31.24
14 -0.02    NA 29.91
15  1.51  9.21 21.65
16 -0.50    NA 21.65
17  1.51    NA 31.24
18 -0.50  9.21    NA
19    NA  9.21 21.65
20  1.51 10.97    NA

To check which values in df1 are NA on the above created data frame, add the following code to the above snippet −

x1<-sample(c(NA,round(rnorm(3),2)),20,replace=TRUE)
x2<-sample(c(NA,round(rnorm(3,10,2),2)),20,replace=TRUE)
x3<-sample(c(NA,round(rnorm(3,25,3.2),2)),20,replace=TRUE)
df1<-data.frame(x1,x2,x3)
apply(df1,2, function(x) is.na(x))

## Output

If you execute all the above given snippets as a single program, it generates the following Output −

x1 x2 x3
[1,]  FALSE FALSE FALSE
[2,]  FALSE FALSE FALSE
[3,]  TRUE  TRUE  FALSE
[4,]  FALSE FALSE TRUE
[5,]  TRUE  TRUE  FALSE
[6,]  TRUE  FALSE FALSE
[7,]  FALSE FALSE FALSE
[8,]  TRUE  TRUE  FALSE
[9,]  FALSE FALSE FALSE
[10,] FALSE TRUE  TRUE
[11,] FALSE FALSE FALSE
[12,] TRUE  TRUE  FALSE
[13,] FALSE FALSE FALSE
[14,] FALSE TRUE  FALSE
[15,] FALSE FALSE FALSE
[16,] FALSE TRUE  FALSE
[17,] FALSE TRUE  FALSE
[18,] FALSE FALSE TRUE
[19,] TRUE  FALSE FALSE
[20,] FALSE FALSE TRUE

## Example 2

Following snippet creates a sample data frame −

y1<-sample(c(NA,rpois(3,1)),20,replace=TRUE)
y2<-sample(c(NA,rpois(2,5)),20,replace=TRUE)
y3<-sample(c(NA,rpois(2,2)),20,replace=TRUE)
df2<-data.frame(y1,y2,y3)
df2

The following dataframe is created

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

To check which values in df2 are NA on the above created data frame, add the following code to the above snippet −

y1<-sample(c(NA,rpois(3,1)),20,replace=TRUE)
y2<-sample(c(NA,rpois(2,5)),20,replace=TRUE)
y3<-sample(c(NA,rpois(2,2)),20,replace=TRUE)
df2<-data.frame(y1,y2,y3)
apply(df2,2, function(x) is.na(x))

## Output

If you execute all the above given snippets as a single program, it generates the following Output −

y1   y2     y3
[1,]  FALSE TRUE  FALSE
[2,]  FALSE FALSE FALSE
[3,]  FALSE TRUE  FALSE
[4,]  FALSE TRUE  FALSE
[5,]  TRUE  TRUE  TRUE
[6,]  TRUE  FALSE FALSE
[7,]  FALSE FALSE FALSE
[8,]  FALSE FALSE FALSE
[9,]  FALSE TRUE  FALSE
[10,] FALSE FALSE TRUE
[11,] FALSE TRUE  TRUE
[12,] FALSE TRUE  FALSE
[13,] FALSE FALSE TRUE
[14,] FALSE FALSE FALSE
[15,] FALSE FALSE FALSE
[16,] FALSE FALSE FALSE
[17,] FALSE FALSE FALSE
[18,] FALSE FALSE FALSE
[19,] FALSE FALSE TRUE
[20,] FALSE TRUE TRUE

Updated on: 03-Nov-2021

14K+ Views