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

R ProgrammingServer Side ProgrammingProgramming

To check if a matrix has any missing value in R, we can use any function along with is.na function.

For Example, if we have a matrix called M then we can use the below command to check whether M contains any missing value or not −

any(is.na(M))

Example 1

Following snippet creates a sample matrix −

M1<-matrix(sample(c(NA,1:10),80,replace=TRUE),ncol=4)
M1

The following matrix is created −

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

To check whether M1 has any NA on the above created matrix, add the following code to the above snippet −

M1<-matrix(sample(c(NA,1:10),80,replace=TRUE),ncol=4)
any(is.na(M1))

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 matrix −

M2<-matrix(rpois(80,10),ncol=4)
M2

The following matrix is created −

     [,1] [,2] [,3] [,4]
[1,]  16    8    9   14
[2,]   8    6    7   12
[3,]   7   12   11    7
[4,]  15   10    8   11
[5,]   9    6    9   12
[6,]  10    6   11   12
[7,]  10   10    6   16
[8,]  10    3    9   12
[9,]  12    5   12   15
[10,] 10    8   10    3
[11,] 11    7    8   11
[12,] 10   15   12    7
[13,] 13    9   15    6
[14,] 14   10    6    5
[15,]  6   16    8   10
[16,] 10   13   19    8
[17,] 21    5    8    9
[18,] 11    6    5   10
[19,] 14   11    4   11
[20,] 10    5    8   10

To check whether M2 has any NA on the above created matrix, add the following code to the above snippet −

M2<-matrix(rpois(80,10),ncol=4)
any(is.na(M2))

Output

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

[1] FALSE

Example 3

Following snippet creates a sample matrix −

M3<-matrix(sample(c(NA,round(rnorm(10),2)),80,replace=TRUE),ncol=4)
M3

The following matrix is created −

        [,1] [,2]  [,3]  [,4]
[1,]   0.65 -1.24  2.39  1.42
[2,]  -0.26  1.42  0.65  0.04
[3,]  -0.69  0.65  0.04    NA
[4,]  -0.26  1.42 -1.14    NA
[5,]  -1.24  0.65 -0.69    NA
[6,]   1.42  0.04  0.65  0.65
[7,]  -0.69 -0.69  0.04  2.39
[8,]     NA -1.14  0.65  1.42
[9,]     NA -0.01  2.39    NA
[10,]  1.42  2.39 -0.26    NA
[11,]  0.65  2.39    NA -0.01
[12,] -1.14 -1.24 -1.24 -0.69
[13,]  1.42  0.65 -0.69  0.04
[14,]  0.04 -0.69  1.42  1.42
[15,]  2.39 -0.26 -0.01 -0.69
[16,] -0.01  2.39 -0.69 -0.26
[17,]  0.04 -0.69 -0.26    NA
[18,] -0.26 -0.34 -1.14  2.39
[19,]  2.39  2.39  2.39 -0.69
[20,]  0.04 -0.34  0.65 -0.69

To check whether M3 has any NA on the above created matrix, add the following code to the above snippet −

M3<-matrix(sample(c(NA,round(rnorm(10),2)),80,replace=TRUE),ncol=4)
any(is.na(M3))

Output

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

[1] TRUE
raja
Updated on 05-Nov-2021 06:41:26

Advertisements