Open In App

How to find missing values in a matrix in R

Last Updated : 12 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will examine various methods for finding missing values in a matrix by using R Programming Language.

What are missing values?

The data points in a dataset that are missing for a particular variable are known as missing values. These missing values are represented in various ways such as Blank spaces, null values, or any special symbols like”NA” or “NaN”.

How do you find missing values in a matrix?

R language offers various methods for finding missing values in a matrix. By using these methods provided by R, it is possible to calculate missing values easily. Some of the methods for finding missing values are:

By using the function is.na()

This function returns the logical values such as ‘True’ or ‘False’. If the position of the element consists ‘NA’, then it returns ‘FALSE’ at that position. Similarly, if the position of the element consist non-zero elements, then it returns ‘TRUE’ at that position.

Syntax:

is.na(matrix)

In the below example, we created the matrix for finding missing values by using the function ‘is.na()’.

R
a=c(1, 20, NA, 4,NA, NA, 9, 4, NA, 9, 2, NA)

# Creating matrix
mat = matrix(a, nrow = 4, ncol=3)
print("The matrix is")
print(mat)

print("Finding missing values in the matrix")
result = is.na(mat)
print(result)

Output:

[1] "The matrix is"
[,1] [,2] [,3]
[1,] 1 NA NA
[2,] 20 NA 9
[3,] NA 9 2
[4,] 4 4 NA

[1] "Finding missing values in the matrix"
[,1] [,2] [,3]
[1,] FALSE TRUE TRUE
[2,] FALSE TRUE FALSE
[3,] TRUE FALSE FALSE
[4,] FALSE FALSE TRUE

In the below example, we created the matrix for finding missing values by using the function ‘is.na()’.

R
# Creating matrix
mat = matrix(c("a","s",NA,"t",NA,"u","s",NA,"c","e",NA,"s","q",NA,"a",NA),ncol=4)
print("The matrix is")
print(mat)

print("Finding missing values in the matrix")
result = is.na(mat)
print(result)

Output:

[1] "The matrix is"
[,1] [,2] [,3] [,4]
[1,] "a" NA "c" "q"
[2,] "s" "u" "e" NA
[3,] NA "s" NA "a"
[4,] "t" NA "s" NA

[1] "Finding missing values in the matrix"
[,1] [,2] [,3] [,4]
[1,] FALSE TRUE FALSE FALSE
[2,] FALSE FALSE FALSE TRUE
[3,] TRUE FALSE TRUE FALSE
[4,] FALSE TRUE FALSE TRUE

By using the which function

The method ‘which()’ is used to return indices or position of the elements, when it is TRUE. It gives you the position of value in logical vector. The position may be in rows or columns.

Syntax:

which( x, arr.ind=T )

In the below example, we created the matrix for finding missing values by using the function ‘which()’.

R
vec1=c(1,2,3,NA,7,NA,23,NA,5,23,NA,NA,6,12,28,NA)

# Creating matrix
mat = matrix(vec1, nrow=4)
print("The matrix is")
print(mat)

print("Finding indexes of the missing values")
result =which(is.na(mat), arr.ind = TRUE)
print(result)

Output:

[1] "The matrix is"
[,1] [,2] [,3] [,4]
[1,] 1 7 5 6
[2,] 2 NA 23 12
[3,] 3 23 NA 28
[4,] NA NA NA NA

[1] "Finding indexes of the missing values"
row col
[1,] 4 1
[2,] 2 2
[3,] 4 2
[4,] 3 3
[5,] 4 3
[6,] 4 4

In the below example, we created the matrix for finding missing values by using the function ‘which()’.

R
a=c("a",NA,"b",NA,"c",NA,"d","e",NA,"f","g",NA)

# Creating matrix
mat = matrix(a, nrow=4)
print("The matrix is")
print(mat)

print("Finding indexes of the missing values")
result =which(is.na(mat), arr.ind = TRUE)
print(result)

Output:

[1] "The matrix is"
[,1] [,2] [,3]
[1,] "a" "c" NA
[2,] NA NA "f"
[3,] "b" "d" "g"
[4,] NA "e" NA

[1] "Finding indexes of the missing values"
row col
[1,] 2 1
[2,] 4 1
[3,] 2 2
[4,] 1 3
[5,] 4 3

Conclusion

In Conclusion, we learned about how to find missing values in a matrix by using R. R language offers versatile tools for finding missing values in the matrix.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads