Skip to content

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
This is probably not the best way but it seems to work.


Ram?n
On Thursday 10 April 2003 15:56, Ramzi Feghali wrote:

  
    
#
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:
#
At 15:56 10/04/2003 +0200, vous avez ?crit:
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
#
Ramon Diaz wrote:
Except that TRUE is 1 and FALSE is 0. The original question was:
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+0
      [,1] [,2]
[1,]    1    0
[2,]    0    1

How's this?
Spencer Graves
Ramzi Feghali wrote:
#
On Thu, 10 Apr 2003, Ramon Diaz wrote:

            
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.