[R] dimension-preserving matrix coersion
i've written a function to coerce a matrix (e.g. from numeric to logical), but i'd like to know if someone has a more elegant method for this: m - matrix(c(0, 1, 1, 0), ncol = 2) m - as.logical(m) m [1] FALSE TRUE TRUE FALSE i'd like 'm' to still be a matrix with the original dimensions. in my function to do this, i coerce 'm' to a logical, then re-form it as a matrix, which seems like an extra (possibly bug-introducing) step that might be avoided if i knew of some hidden feature that might permit this in one fell swoop. any ideas? __ 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.
Re: [R] dimension-preserving matrix coersion
How about this: m - matrix(c(0, 1, 1, 0), ncol = 2) mode(m) - 'logical' m [,1] [,2] [1,] FALSE TRUE [2,] TRUE FALSE On Sun, Sep 27, 2009 at 6:59 PM, Murat Tasan mmu...@gmail.com wrote: i've written a function to coerce a matrix (e.g. from numeric to logical), but i'd like to know if someone has a more elegant method for this: m - matrix(c(0, 1, 1, 0), ncol = 2) m - as.logical(m) m [1] FALSE TRUE TRUE FALSE i'd like 'm' to still be a matrix with the original dimensions. in my function to do this, i coerce 'm' to a logical, then re-form it as a matrix, which seems like an extra (possibly bug-introducing) step that might be avoided if i knew of some hidden feature that might permit this in one fell swoop. any ideas? __ 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. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? __ 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.
Re: [R] dimension-preserving matrix coersion
HA! yeah, that'll do it! forgot that mode() can be used to set modes as well as get them. thanks much! -murat On Sep 27, 7:10 pm, jim holtman jholt...@gmail.com wrote: How about this: m - matrix(c(0, 1, 1, 0), ncol = 2) mode(m) - 'logical' m [,1] [,2] [1,] FALSE TRUE [2,] TRUE FALSE On Sun, Sep 27, 2009 at 6:59 PM, Murat Tasan mmu...@gmail.com wrote: i've written a function to coerce a matrix (e.g. from numeric to logical), but i'd like to know if someone has a more elegant method for this: m - matrix(c(0, 1, 1, 0), ncol = 2) m - as.logical(m) m [1] FALSE TRUE TRUE FALSE i'd like 'm' to still be a matrix with the original dimensions. in my function to do this, i coerce 'm' to a logical, then re-form it as a matrix, which seems like an extra (possibly bug-introducing) step that might be avoided if i knew of some hidden feature that might permit this in one fell swoop. any ideas? __ r-h...@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? __ r-h...@r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ 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.
Re: [R] dimension-preserving matrix coersion
-Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Murat Tasan Sent: Sunday, September 27, 2009 3:59 PM To: r-help@r-project.org Subject: [R] dimension-preserving matrix coersion i've written a function to coerce a matrix (e.g. from numeric to logical), but i'd like to know if someone has a more elegant method for this: m - matrix(c(0, 1, 1, 0), ncol = 2) m - as.logical(m) m [1] FALSE TRUE TRUE FALSE i'd like 'm' to still be a matrix with the original dimensions. in my function to do this, i coerce 'm' to a logical, then re-form it as a matrix, which seems like an extra (possibly bug-introducing) step that might be avoided if i knew of some hidden feature that might permit this in one fell swoop. m - m != 0 will do it, as will mode(m) - logical The former has the advantage that you don't have to use the canonical mapping of 0-FALSE,anything else-TRUE, but can use any standard comparison or vectorized logical operator. It also seems cleaner in that it can be used inside a larger expression, like m[m!=0]. Bill Dunlap TIBCO Software Inc - Spotfire Division wdunlap tibco.com any ideas? __ 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. __ 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.