# [R] a general way to select a subset of matrix rows?

Stephanie Kovalchik skoval at ucla.edu
Sun May 10 19:43:58 CEST 2009

```One simple adjustment is the following:

apply(matrix(x[rows,],nr=length(rows)),2,mean)

Quoting Peter Kharchenko <peter.kharchenko at post.harvard.edu>:

> 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 properly.
>
>  Here's an example:
> # define a 3-by-4 matrix x
>> x <- matrix(runif(12),ncol=4)
>> str(x)
> 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)
>> apply(x[rows,],2,mean)
>  0.3923531 0.7552746 0.3661532 0.1069531
> # now the same thing, but the "rows" vector is actually just one row
>> rows <- c(2)
>> apply(x[rows,],2,mean)
> 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?
>
> thanks,
> -peter.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help