[R] a general way to select a subset of matrix rows?
peter.kharchenko at post.harvard.edu
Sat May 9 19:33:58 CEST 2009
Dear fellow R users,
I can't figure out how to do a simple thing properly: apply an operation
to matrix columns on a selected subset of rows. Things go wrong when
only one row is being selected. I am sure there's a way to do this
Here's an example:
# define a 3-by-4 matrix x
> x <- matrix(runif(12),ncol=4)
num [1:3, 1:4] 0.568 0.217 0.309 0.859 0.651 ...
# calculate column means for selected rows
> rows <- c(1,2)
 0.3923531 0.7552746 0.3661532 0.1069531
# now the same thing, but the "rows" vector is actually just one row
> rows <- c(2)
Error in apply(x[rows, ], 2, mean) : dim(X) must have a positive length
The problem is that while x[rows,] in the first case returned a matrix,
in the second case, when only one row was selected, it returned a vector
(and the apply obviously failed). Is there a general way to subset a
matrix so it still returns a matrix even if it's one row?
Unfortunately doing as.matrix(x[rows,]) doesn't work either, as it
returns a transposed matrix in the case of a single row.
Is there a way to do this properly without writing out hideous if
statements accounting for single row exception?
More information about the R-help