Possible Duplicate:
Replace all 0 values to NA in R
Going off of this question. Is there a similar function in R such as x[is.na(x)] <- 0
except that it will changes every zero in a matrix to NA?
You can do it like this:
x[x == 0] <- NA
For example:
x = matrix(rep(0:1, 50), nrow=10)
x[x == 0] <- NA
print(x)
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# [1,] NA NA NA NA NA NA NA NA NA NA
# [2,] 1 1 1 1 1 1 1 1 1 1
# [3,] NA NA NA NA NA NA NA NA NA NA
# [4,] 1 1 1 1 1 1 1 1 1 1
# [5,] NA NA NA NA NA NA NA NA NA NA
# [6,] 1 1 1 1 1 1 1 1 1 1
# [7,] NA NA NA NA NA NA NA NA NA NA
# [8,] 1 1 1 1 1 1 1 1 1 1
# [9,] NA NA NA NA NA NA NA NA NA NA
#[10,] 1 1 1 1 1 1 1 1 1 1