Table of Contents
In R, NA values refer to missing or undefined data in a matrix. To remove these NA values from a matrix, the function “na.omit()” can be used. This function will remove any rows that contain NA values in the specified matrix. An example of using this function would be as follows:
> #creating a matrix with NA values
> matrix matrix
[,1] [,2] [,3]
[1,] 1 4 8
[2,] 2 NA 9
[3,] 3 6 NA
[4,] NA 7 10
> #removing NA values from the matrix
> new_matrix new_matrix
[,1] [,2] [,3]
[1,] 1 4 8
[2,] 2 NA 9
This function is useful for cleaning and preparing data for analysis, as it removes any incomplete or irrelevant rows from the matrix.
Remove NA from Matrix in R (With Example)
You can use the following methods to remove NA values from a matrix in R:
Method 1: Remove Rows with NA Values
new_matrix <- my_matrix[!rowSums(is.na(my_matrix)),]
Method 2: Remove Columns with NA Values
new_matrix <- my_matrix[, !colSums(is.na(my_matrix))]
The following examples show how to use each method in practice with the following matrix in R:
#create matrix my_matrix <- matrix(c(NA, 0, NA, 5, 7, 4, 1, 3, 9, 5, 5, 8), nrow=4) #view matrix my_matrix [,1] [,2] [,3] [1,] NA 7 9 [2,] 0 4 5 [3,] NA 1 5 [4,] 5 3 8
Method 1: Remove Rows with NA Values
The following code shows how to remove all rows from the matrix that contain NA values:
#remove all rows with NA values
new_matrix <- my_matrix[!rowSums(is.na(my_matrix)),]
#view updated matrix
new_matrix
[,1] [,2] [,3]
[1,] 0 4 5
[2,] 5 3 8Notice that all rows with NA values have been removed from the matrix.
Related:
Method 2: Remove Columns with NA Values
The following code shows how to remove all columns from the matrix that contain NA values:
#remove all columns with NA values
new_matrix <- my_matrix[, !colSums(is.na(my_matrix))]
#view updated matrix
new_matrix
[,1] [,2]
[1,] 7 9
[2,] 4 5
[3,] 1 5
[4,] 3 8Notice that all columns with NA values have been removed from the matrix.
Related:
Bonus: Convert NA Values to Zero in Matrix
If you simply want to convert all NA values to zero in a matrix, you can use the following syntax:
#remove all columns with NA values
my_matrix[is.na(my_matrix)] <- 0
#view updated matrix
my_matrix
[,1] [,2] [,3]
[1,] 0 7 9
[2,] 0 4 5
[3,] 0 1 5
[4,] 5 3 8Notice that all NA values have been converted to zero.
The following tutorials explain how to perform other common operations with missing values in R:
Cite this article
stats writer (2024). How can I remove NA values from a matrix in R, with an example?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-remove-na-values-from-a-matrix-in-r-with-an-example/
stats writer. "How can I remove NA values from a matrix in R, with an example?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-remove-na-values-from-a-matrix-in-r-with-an-example/.
stats writer. "How can I remove NA values from a matrix in R, with an example?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-remove-na-values-from-a-matrix-in-r-with-an-example/.
stats writer (2024) 'How can I remove NA values from a matrix in R, with an example?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-remove-na-values-from-a-matrix-in-r-with-an-example/.
[1] stats writer, "How can I remove NA values from a matrix in R, with an example?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I remove NA values from a matrix in R, with an example?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
