How to check if a data frame has any missing value in R?

R ProgrammingServer Side ProgrammingProgramming

To check if a data frame has any missing value in R, we can use any function along with is.na function. For Example, if we have a data frame called df then we can use the below command to check whether df contains any missing value or not

any(is.na(df))

Example 1

Following snippet creates a sample data frame −

x1<-rpois(20,5)
x2<-sample(c(NA,2,6),20,replace=TRUE)
df1<-data.frame(x1,x2)
df1

The following dataframe is created −

x1  x2
 1  4  6
 2  8  6
 3  5  6
 4  5  2
 5  6 NA
 6  5  6
 7  3 NA
 8  4  2
 9  9 NA
10  2  6
11  6 NA
12  6 NA
13  6  6
14  6 NA
15  3  6
16  5  6
17  5 NA
18  4  2
19  2  6
20  3 NA

To check whether df1 has any NA on the above created data frame, add the following code to the above snippet −

x1<-rpois(20,5)
x2<-sample(c(NA,2,6),20,replace=TRUE)
df1<-data.frame(x1,x2)
any(is.na(df1))

Output

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

[1] TRUE

Example 2

Following snippet creates a sample data frame −

y1<-sample(c(NA,rnorm(5)),20,replace=TRUE)
y2<-rnorm(20)
df2<-data.frame(y1,y2)
df2

The following dataframe is created −

       y1         y2
1  -1.4175108  1.8349444
2  -2.7647068 -0.6014623
3          NA -0.8020289
4  -1.0745120 -0.8106467
5  -2.7647068  0.6680208
6  -2.7647068 -0.5579063
7         NA   1.7695050
8  -1.0745120 -0.1914589
9   0.4555854  0.5047105
10 -1.4175108 -0.6461347
11 -1.0745120  0.7005221
12 -1.0745120  1.9436422
13  0.4555854 -0.4179736
14  0.4555854 -0.2962887
15  0.5136818  1.5961105
16 -1.4175108  0.6244578
17 -1.0745120 -0.4413115
18  0.5136818 -1.4493746
19  0.5136818 -0.1654110
20 -2.7647068  0.7870973

To check whether df2 has any NA on the above created data frame, add the following code to the above snippet −

y1<-sample(c(NA,rnorm(5)),20,replace=TRUE)
y2<-rnorm(20)
df2<-data.frame(y1,y2)
any(is.na(df2))

Output

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

[1] TRUE

Example 3

Following snippet creates a sample data frame −

z1<-runif(20,1,5)
z2<-runif(20,1,5)
df3<-data.frame(z1,z2)
df3

The following dataframe is created −

      z1       z2
1  4.199921  3.974369
2  1.362028  2.467372
3  3.970619  4.989726
4  1.969228  4.985778
5  4.238796  1.545944
6  3.406546  3.301960
7  2.345338  1.012634
8  2.181524  4.013063
9  2.187973  3.121378
10 2.136693  3.059296
11 3.430986  1.243260
12 1.427495  1.387059
13 4.714494  3.976311
14 3.235821  3.264096
15 4.604128  4.383884
16 1.398644  3.596508
17 3.139503  1.853239
18 1.764061  3.128764
19 3.234675  3.491583
20 4.461674  3.580696

To check whether df3 has any NA on the above created data frame, add the following code to the above snippet −

z1<-runif(20,1,5)
z2<-runif(20,1,5)
df3<-data.frame(z1,z2)
any(is.na(df3))

Output

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

[1] FALSE
raja
Published on 27-Oct-2021 12:06:56

Advertisements