I have encountered some strange behaviour in R. Suppose I have a matrix and I want to remove a specified set of rows and columns. Here is an example where this works perfectly well.
#Create a matrix
MATRIX <- matrix(1:50, nrow = 4, ncol = 5)
rownames(MATRIX) <- c('a', 'b', 'c', 'd')
colnames(MATRIX) <- c('a', 'b', 'c', 'd', 'e')
#Specify rows and columns to remove
REMOVE.ROW <- 3
REMOVE.COL <- 2
#Print the matrix without these rows or columns
MATRIX[-REMOVE.ROW, -REMOVE.COL]
a c d e
a 1 9 13 17
b 2 10 14 18
d 4 12 16 20
However, when one or both of the objects REMOVE.ROW or REMOVE.COL are empty, instead of removing nothing (and therefore giving back the original matrix), it gives me back an empty matrix.
#Specify rows and columns to remove
REMOVE.ROW <- integer(0)
REMOVE.COL <- integer(0)
#Print the matrix without these rows or columns
MATRIX[-REMOVE.ROW, -REMOVE.COL]
<0 x 0 matrix>
Intuitively, I would have expected the removal of an empty set of indices to leave me with the original set of indices, and so I would have expected the full matrix back from this command. For some reason, R removes all rows and columns from the matrix in this case. As far as I can make out, this appears to be a bug in R, but perhaps there is some good reason for it that I am unaware of.
Question: Can someone explain why R is doing things this way? Aside from using if-then statements to deal with the special cases, is there any simple adjustment I can make to have R behave as I want it to?