Let us assume that there is a matrix in R and you would like to remove both the
columns and rows with NA values in it. Following is the code
1) Create a matrix with 4 columns and the values are from 1 to 16
x = matrix(seq(16), ncol = 4)
2) Now change the value in 2nd row, 3rd column as NA.
x[2,3]= NA
3) Now print the matrix
> x
[,1] [,2] [,3] [,4]
[1,] 1 5 9 13
[2,] 2 6 NA 14
[3,] 3 7 11 15
[4,] 4 8 12 16
4)To remove rows and columns with NA:
> x[complete.cases(x),complete.cases(t(x))]
[,1] [,2] [,3]
[1,] 1 5 13
[2,] 3 7 15
[3,] 4 8 16