[R] Transforming matrix

Barry Rowlingson B.Rowlingson at lancaster.ac.uk
Thu Apr 10 16:20:41 CEST 2003


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



More information about the R-help mailing list