We can use diag() to remove diagonal elements of a matrix, but what if we want to remove a diagonal square of elements? Like in a 6x6 matrix, I want to remove 2x2 squares in the diagonal. It looks very basic, but how to do that in r?
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 23 98 12 98 32 99
[2,] 54 11 13 02 31 78
[3,] 25 85 15 09 46 87
[4,] 98 98 16 17 45 48
[5,] 88 00 68 99 89 89
[6,] 05 33 66 12 14 78
and I want to set the diagonal square to NA
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] NA NA 12 98 32 99
[2,] NA NA 13 02 31 78
[3,] 25 85 NA NA 46 87
[4,] 98 98 NA NA 45 48
[5,] 88 00 68 99 NA NA
[6,] 05 33 66 12 NA NA
remove all the NAs and then we combine the columns
[,1] [,2] [,3] [,4]
[1,] 12 98 32 99
[2,] 13 02 31 78
[3,] 25 85 46 87
[4,] 98 98 45 48
[5,] 88 00 68 99
[6,] 05 33 66 12