Given is a matrix (no assumptions are made about its dimensions) which contains zeros below the diagonal that runs from the bottom left to the upper right corner:
m <- matrix(data = c(2, 1, 8, 9, 1,
5, 0, 4, 3, 6,
7, 1, 2, 5, 0,
3, 1, 0, 0, 0,
2, 8, 0, 0, 0,
9, 0, 0, 0, 0),
nrow = 6,
ncol = 5,
byrow = TRUE)
The question is how to replace all these zeros below the diagonal with NA
? The result should be the following:
[,1] [,2] [,3] [,4] [,5]
[1,] 2 1 8 9 1
[2,] 5 0 4 3 6
[3,] 7 1 2 5 NA
[4,] 3 1 0 NA NA
[5,] 2 8 NA NA NA
[6,] 9 NA NA NA NA
The matrix may contain zeros elsewhere (on or above the diagonal). These zeros should not be replaced.