Austin Huang
2009-Dec-04 20:52 UTC
[R] logical masking of a matrix converts it to a vector
One problem I've been having is the special case in which only one row/column remains and the variable gets converted into a vector when entries are removed by logical masking. This is a problem because subsequent code may rely on matrix operations (apply, colsums, dim, etc) For example:> a <- matrix(c(1, 2, 3, 4), nrow = 2) > a[,1] [,2] [1,] 1 3 [2,] 2 4> rmask <- c(TRUE, FALSE) > b <- a[rmask, ] > colSums(b)Error in colSums(b) : 'x' must be an array of at least two dimensions To ensure the code works regardless of how the matrix gets modified, I need to explicitly recast results to a matrix, for example: b <- matrix(a[rmask, ], ncol = 2) This can get messy, requiring extra work to maintain column/row names or additional commands to determine the correct dimensions of the matrix (for example, if I'm masking both rows and columns simultaneously). Is there a more elegant way to deal with this in R? ~A [[alternative HTML version deleted]]
David Winsemius
2009-Dec-04 21:00 UTC
[R] logical masking of a matrix converts it to a vector
On Dec 4, 2009, at 3:52 PM, Austin Huang wrote:> One problem I've been having is the special case in which only one > row/column remains and the variable gets converted into a vector when > entries are removed by logical masking. This is a problem because > subsequent > code may rely on matrix operations (apply, colsums, dim, etc) For > example: > >> a <- matrix(c(1, 2, 3, 4), nrow = 2) >> a > [,1] [,2] > [1,] 1 3 > [2,] 2 4 >> rmask <- c(TRUE, FALSE) >> b <- a[rmask, ] >> colSums(b)> b <- a[rmask, , drop=FALSE] > colSums(b) [1] 1 3> Error in colSums(b) : 'x' must be an array of at least two dimensions > > To ensure the code works regardless of how the matrix gets modified, > I need > to explicitly recast results to a matrix, for example: > > b <- matrix(a[rmask, ], ncol = 2) > > This can get messy, requiring extra work to maintain column/row > names or > additional commands to determine the correct dimensions of the > matrix (for > example, if I'm masking both rows and columns simultaneously). Is > there a > more elegant way to deal with this in R?-- David Winsemius, MD Heritage Laboratories West Hartford, CT
Ravi Varadhan
2009-Dec-04 21:00 UTC
[R] logical masking of a matrix converts it to a vector
Check out `drop' ?drop a <- matrix(c(1, 2, 3, 4), nrow = 2) rmask <- c(TRUE, FALSE) b <- a[rmask, , drop=FALSE] colSums(b) This will maintain the matrix structure. Ravi. ---------------------------------------------------------------------------- ------- Ravi Varadhan, Ph.D. Assistant Professor, The Center on Aging and Health Division of Geriatric Medicine and Gerontology Johns Hopkins University Ph: (410) 502-2619 Fax: (410) 614-9625 Email: rvaradhan at jhmi.edu Webpage: http://www.jhsph.edu/agingandhealth/People/Faculty_personal_pages/Varadhan.h tml ---------------------------------------------------------------------------- -------- -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Austin Huang Sent: Friday, December 04, 2009 3:52 PM To: R-help at r-project.org Subject: [R] logical masking of a matrix converts it to a vector One problem I've been having is the special case in which only one row/column remains and the variable gets converted into a vector when entries are removed by logical masking. This is a problem because subsequent code may rely on matrix operations (apply, colsums, dim, etc) For example:> a <- matrix(c(1, 2, 3, 4), nrow = 2) > a[,1] [,2] [1,] 1 3 [2,] 2 4> rmask <- c(TRUE, FALSE) > b <- a[rmask, ] > colSums(b)Error in colSums(b) : 'x' must be an array of at least two dimensions To ensure the code works regardless of how the matrix gets modified, I need to explicitly recast results to a matrix, for example: b <- matrix(a[rmask, ], ncol = 2) This can get messy, requiring extra work to maintain column/row names or additional commands to determine the correct dimensions of the matrix (for example, if I'm masking both rows and columns simultaneously). Is there a more elegant way to deal with this in R? ~A [[alternative HTML version deleted]] ______________________________________________ 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.
Erik Iverson
2009-Dec-04 21:00 UTC
[R] logical masking of a matrix converts it to a vector
> One problem I've been having is the special case in which only one > row/column remains and the variable gets converted into a vector when > entries are removed by logical masking. This is a problem because > subsequent > code may rely on matrix operations (apply, colsums, dim, etc) For example: > > > a <- matrix(c(1, 2, 3, 4), nrow = 2) > > a > [,1] [,2] > [1,] 1 3 > [2,] 2 4 > > rmask <- c(TRUE, FALSE) > > b <- a[rmask, ] > > colSums(b) > Error in colSums(b) : 'x' must be an array of at least two dimensions > > To ensure the code works regardless of how the matrix gets modified, I > need > to explicitly recast results to a matrix, for example: > > b <- matrix(a[rmask, ], ncol = 2) > > This can get messy, requiring extra work to maintain column/row names or > additional commands to determine the correct dimensions of the matrix (for > example, if I'm masking both rows and columns simultaneously). Is there a > more elegant way to deal with this in R?See ?[ and ?drop b <- a[rmask, , drop = FALSE]
Jorge Ivan Velez
2009-Dec-04 21:03 UTC
[R] logical masking of a matrix converts it to a vector
Hi Austin, What version of R are you using? It works for me for R 2.10.0 Patched on Win XP Pro: R> a <- matrix(c(1, 2, 3, 4), nrow = 2) R> a # [1] 1 3 # [2] 2 4 R> rmask <- c(TRUE, FALSE) R> a[rmask,] # [1] 1 3 HTH, Jorge On Fri, Dec 4, 2009 at 3:52 PM, Austin Huang <> wrote:> One problem I've been having is the special case in which only one > row/column remains and the variable gets converted into a vector when > entries are removed by logical masking. This is a problem because > subsequent > code may rely on matrix operations (apply, colsums, dim, etc) For example: > > > a <- matrix(c(1, 2, 3, 4), nrow = 2) > > a > [,1] [,2] > [1,] 1 3 > [2,] 2 4 > > rmask <- c(TRUE, FALSE) > > b <- a[rmask, ] > > colSums(b) > Error in colSums(b) : 'x' must be an array of at least two dimensions > > To ensure the code works regardless of how the matrix gets modified, I need > to explicitly recast results to a matrix, for example: > > b <- matrix(a[rmask, ], ncol = 2) > > This can get messy, requiring extra work to maintain column/row names or > additional commands to determine the correct dimensions of the matrix (for > example, if I'm masking both rows and columns simultaneously). Is there a > more elegant way to deal with this in R? > > ~A > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >[[alternative HTML version deleted]]
Phil Spector
2009-Dec-04 21:04 UTC
[R] logical masking of a matrix converts it to a vector
b <- a[rmask,,drop=FALSE] - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Fri, 4 Dec 2009, Austin Huang wrote:> One problem I've been having is the special case in which only one > row/column remains and the variable gets converted into a vector when > entries are removed by logical masking. This is a problem because subsequent > code may rely on matrix operations (apply, colsums, dim, etc) For example: > >> a <- matrix(c(1, 2, 3, 4), nrow = 2) >> a > [,1] [,2] > [1,] 1 3 > [2,] 2 4 >> rmask <- c(TRUE, FALSE) >> b <- a[rmask, ] >> colSums(b) > Error in colSums(b) : 'x' must be an array of at least two dimensions > > To ensure the code works regardless of how the matrix gets modified, I need > to explicitly recast results to a matrix, for example: > > b <- matrix(a[rmask, ], ncol = 2) > > This can get messy, requiring extra work to maintain column/row names or > additional commands to determine the correct dimensions of the matrix (for > example, if I'm masking both rows and columns simultaneously). Is there a > more elegant way to deal with this in R? > > ~A > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >