I would add that indexing a matrix or array automatically drops dimensions of size 1. This can occasionally be convenient, as with a multidimensional array with a lot of useless dimensions, but in my experience it causes problems more often than not. If you subset only one row/column of a matrix, the result is just a vector, and functions that require a matrix (e.g. rowMeans) will obviously fail:
a <- matrix(seq(12),nr=3,nc=4)
rowMeans(a[1:2,])
[1] 5.5 6.5
rowMeans(a[1,])
Error in rowMeans(a[1, ]) :
'x' must be an array of at least two dimensions
Even if you explicitly create a 1-row matrix, subsetting will convert to vector:
This is intended behavior, but I find that I often want to explicitly keep all dimensions. For example, if you write a function that usually processes matrices with 2+ rows, but then one day pass it a one-row matrix, the first subsetting operation will convert it to a vector and will cause subsequent matrix operations to fail. Therefore you either need to explicitly handle operations on the one-row/one-column matrix separately, or you can make a habit of adding a "drop" parameter to the index, as I have done:
a <- matrix(seq(12),nr=3,nc=4)
rowMeans(a[1,,drop=F])
[1] 5.5
I would add that indexing a matrix or array automatically drops dimensions of size 1. This can occasionally be convenient, as with a multidimensional array with a lot of useless dimensions, but in my experience it causes problems more often than not. If you subset only one row/column of a matrix, the result is just a vector, and functions that require a matrix (e.g. rowMeans) will obviously fail:
Even if you explicitly create a 1-row matrix, subsetting will convert to vector:
This is intended behavior, but I find that I often want to explicitly keep all dimensions. For example, if you write a function that usually processes matrices with 2+ rows, but then one day pass it a one-row matrix, the first subsetting operation will convert it to a vector and will cause subsequent matrix operations to fail. Therefore you either need to explicitly handle operations on the one-row/one-column matrix separately, or you can make a habit of adding a "drop" parameter to the index, as I have done: