hi everybody, anyone knows how we can transform a binary matrix with TRUE and FALSE to 0 and 1 without looping? Thx --------------------------------- [[alternate HTML version deleted]]
Dear Ramzi, If you have your TRUE/FALSE values in m1, then do> mode(m1) <- "numeric".This is probably not the best way but it seems to work. Ram?n On Thursday 10 April 2003 15:56, Ramzi Feghali wrote:> hi everybody, > anyone knows how we can transform a binary matrix with TRUE and FALSE to 0 > and 1 without looping? Thx > > > > --------------------------------- > > > [[alternate HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help-- Ram?n D?az-Uriarte Bioinformatics Unit Centro Nacional de Investigaciones Oncol?gicas (CNIO) (Spanish National Cancer Center) Melchor Fern?ndez Almagro, 3 28029 Madrid (Spain) Fax: +-34-91-224-6972 Phone: +-34-91-224-6900 http://bioinfo.cnio.es/~rdiaz
Assuming that TRUE==1 and FALSE==0, just do > x = matrix(c(TRUE,TRUE,FALSE,FALSE),2,2) > x [,1] [,2] [1,] TRUE FALSE [2,] TRUE FALSE > as.numeric(x) [1] 1 1 0 0 > array(as.numeric(x),dim=dim(x)) [,1] [,2] [1,] 1 0 [2,] 1 0 Ramzi Feghali wrote:> hi everybody, > anyone knows how we can transform a binary matrix with TRUE and FALSE to 0 and 1 without looping? > Thx > > > > --------------------------------- > > > [[alternate HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >
storage.mode(mat) <- "integer" or d <- dim(mat) mat <- as.integer(mat) dim(mat) <- d HTH, Andy> -----Original Message----- > From: Ramzi Feghali [mailto:ramzi_feg at yahoo.fr] > Sent: Thursday, April 10, 2003 9:57 AM > To: r-help at stat.math.ethz.ch > Subject: [R] Transforming matrix > > > > hi everybody, > anyone knows how we can transform a binary matrix with TRUE > and FALSE to 0 and 1 without looping? > Thx > > > > --------------------------------- > > > [[alternate HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >------------------------------------------------------------------------------
At 15:56 10/04/2003 +0200, vous avez ?crit:>hi everybody, >anyone knows how we can transform a binary matrix with TRUE and FALSE to 0 >and 1 without looping? >ThxI guess you mean: TRUE ==> 1 FALSE ==> 0 as this is the rule when logicals are converted into numerics. Then, if M is your matrix, you can do: mode(M) <- "numeric" If you actually want: TRUE ==> 0 FALSE ==> 1 then: M <- !M mode(M) <- "numeric" should do it. HTH EP>--------------------------------- > > > [[alternate HTML version deleted]] > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> A <- array(c(T,F,F,T), dim=c(2,2))> A+0 [,1] [,2] [1,] 1 0 [2,] 0 1 How's this? Spencer Graves Ramzi Feghali wrote:> hi everybody, > anyone knows how we can transform a binary matrix with TRUE and FALSE to 0 and 1 without looping? > Thx > > > > --------------------------------- > > > [[alternate HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
On Thu, 10 Apr 2003, Ramon Diaz wrote:> If you have your TRUE/FALSE values in m1, then do > > > mode(m1) <- "numeric". > > This is probably not the best way but it seems to work.That maps TRUE to 1 and FALSE to 0, and I read the request as for the reverse. ifelse(m1, 0, 1) is probably the most transparent way.> On Thursday 10 April 2003 15:56, Ramzi Feghali wrote: > > hi everybody, > > anyone knows how we can transform a binary matrix with TRUE and FALSE to 0 > > and 1 without looping? Thx-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595