How to find missing values in a matrix in R
Last Updated :
12 Apr, 2024
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.
Similar Reads
How to find missing values in a list in R Missing values are frequently encountered in data analysis. In R Programming Language effectively dealing with missing data is critical for correct analysis and interpretation. Whether you're a seasoned data scientist or a new R user, understanding how to identify missing values is critical. In this
3 min read
How to find missing values in a factor in R Missing values are a regular occurrence in data analysis, and they might limit the precision and trustworthiness of your findings. When working with factors in R, the process gets considerably more complex. Have no fear! This article is your guide through the maze of missing values in R factors. We'
2 min read
How to Impute Missing Values in R? In this article, we will discuss how to impute missing values in R programming language. In most datasets, there might be missing values either because it wasn't entered or due to some error. Replacing these missing values with another value is known as Data Imputation. There are several ways of imp
3 min read
How to Find and Count Missing Values in R DataFrame In R programming, missing values are commonly represented as NA. To identify and handle these values effectively, we can use the is.na() function, which checks whether a data point is missing.Syntaxwhich(is.na(data))sum(is.na(data))Parameters:is.na(data): Identifies missing values and returns TRUE f
3 min read
How to find length of matrix in R In this article, we will examine various methods to find the length of a matrix by using R Programming Language. What is a matrix?A matrix is a two-dimensional data structure that is a collection of rows and columns. A matrix can able to contain data of various types such as numeric, characters, and
4 min read
How to Calculate Correlation in R with Missing Values When calculating correlation in R, missing values are excluded by default using a method called pairwise deletion. This means R ignores any observation where a variable in the pair is missing.How to Calculate Correlation in R with Missing ValuesThere are several ways to calculate correlation in R wh
3 min read