Skip to content
Back to formatted view

Raw Message

Message-ID: <3E957DB9.9080601@lancaster.ac.uk>
Date: 2003-04-10T14:20:41Z
From: Barry Rowlingson
Subject: Transforming matrix
In-Reply-To: <200304101612.43831.rdiaz@cnio.es>

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