Peter Kharchenko
2009-May-09 17:33 UTC
[R] a general way to select a subset of matrix rows?
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) [1] 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.
Henrique Dallazuanna
2009-May-09 21:45 UTC
[R] a general way to select a subset of matrix rows?
Yes, use the drop argument; apply(x[rows,,drop=F],2,mean) On Sat, May 9, 2009 at 2:33 PM, Peter Kharchenko < peter.kharchenko@post.harvard.edu> wrote:> 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) > [1] 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@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Stephanie Kovalchik
2009-May-10 17:43 UTC
[R] a general way to select a subset of matrix rows?
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) > [1] 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 > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Maybe Matching Threads
- R vs SPSS output for princomp
- Re: Could not destroy domain, current job is remoteDispatchConnectGetAllDomainStats
- assigning from multiple return values
- Could not destroy domain, current job is remoteDispatchConnectGetAllDomainStats
- [PATCH v35 1/5] mm: support to get hints of free page blocks