Use ifelse() to replace values in a matrix.
From R Help:
ifelse returns a value with the same shape as test which is filled with elements selected from either yes or no depending on whether the element of test is TRUE or FALSE.
Usage
Arguments
test an object which can be coerced to logical mode.
yes return values for true elements of test.
no return values for false elements of test.
Example:
> x=matrix(c(1,1,1,1,-1,-1,-1,-1),ncol=2)
> x
[,1] [,2]
[1,] 1 -1
[2,] 1 -1
[3,] 1 -1
[4,] 1 -1
> x=ifelse(x==-1,0,x)
> x
[,1] [,2]
[1,] 1 0
[2,] 1 0
[3,] 1 0
[4,] 1 0