Matrix A has some zeros and ones in its first row.
A <- matrix(data=c( 0,0,1,1,0,0,0,1,0, #1
0,0,0,0,1,0,0,0,0, #2
1,0,0,0,0,0,0,0,0, #3
1,0,0,0,0,0,0,0,1, #4
0,1,0,0,0,1,0,0,0, #5
0,0,0,0,1,0,0,0,0, #6
0,0,0,0,0,0,0,0,0, #7
1,0,0,0,0,0,0,0,0, #8
0,0,0,1,0,0,0,0,0 ),#9
nrow=9, ncol=9)
I need to replace the zeros in only this row with NA values. The result should look like this:
result <- matrix(data=c( NA,NA,1,1,NA,NA,NA,1,NA, #1
0,0,0,0,1,0,0,0,0, #2
1,0,0,0,0,0,0,0,0, #3
1,0,0,0,0,0,0,0,1, #4
0,1,0,0,0,1,0,0,0, #5
0,0,0,0,1,0,0,0,0, #6
0,0,0,0,0,0,0,0,0, #7
1,0,0,0,0,0,0,0,0, #8
0,0,0,1,0,0,0,0,0 ),#9
nrow=9, ncol=9)
Other solutions to similar problems that fully replace a given value in the entire matrix with another value do not apply, and I'm surprised that I haven't been able to extend these solutions.
CodePudding user response:
A[1, A[1,] == 0] <- NA
A
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
# [1,] NA NA 1 1 NA NA NA 1 NA
# [2,] 0 0 0 0 1 0 0 0 0
# [3,] 1 0 0 0 0 0 0 0 0
# [4,] 1 0 0 0 0 0 0 0 1
# [5,] 0 1 0 0 0 1 0 0 0
# [6,] 0 0 0 0 1 0 0 0 0
# [7,] 0 0 0 0 0 0 0 0 0
# [8,] 1 0 0 0 0 0 0 0 0
# [9,] 0 0 0 1 0 0 0 0 0
