0

I want to index a matrix m*n by specifing row and column but when the m or n becomes 1, you cant index it as a matrix but as a vector:

M=matrix(c(1,2,3,4),nrow=2)
M[1,] #Works
M=M[-1,]
M[1,] #Doesn't work

Is there any workaround instead of using if/else to check for the length of m/n?

Squeezie
  • 361
  • 2
  • 14
  • 1
    You can use the `drop` argument to avoid losing a dimension. E.g. `M[-1, drop = FALSE]` returns a matrix while `M[-1, ]` is coerced to a vector. See `?"["` for details. – Scarabee Mar 02 '18 at 10:18
  • I meant: `M[-1, , drop = FALSE]` (the second comma matters). – Scarabee Mar 02 '18 at 10:43

0 Answers0