An embedded and charset-unspecified text was scrubbed... Name: not available Url: https://stat.ethz.ch/pipermail/r-help/attachments/20030410/3d7c1089/attachment.pl
Transforming matrix
7 messages · Ramzi Feghali, Ramon Diaz-Uriarte, Sundar Dorai-Raj +4 more
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
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? Thx
I 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
Ramon Diaz wrote:
Dear Ramzi, If you have your TRUE/FALSE values in m1, then do
mode(m1) <- "numeric".
Except that TRUE is 1 and FALSE is 0. The original question was:
anyone knows how we can transform a binary matrix with TRUE and FALSE to 0 and 1 without looping?
which could imply they want TRUE mapped to 0, and FALSE to 1, which is
done with '1-':
> foo
[,1] [,2] [,3] [,4] [,5]
[1,] FALSE TRUE FALSE FALSE TRUE
[2,] TRUE TRUE TRUE FALSE FALSE
> 1-foo
[,1] [,2] [,3] [,4] [,5]
[1,] 1 0 1 1 0
[2,] 0 0 0 1 1
>
R will convert TRUE to 1 and FALSE to 0 in a numeric context:
> foo*1
[,1] [,2] [,3] [,4] [,5]
[1,] 0 1 0 0 1
[2,] 1 1 1 0 0
Baz
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