Skip to content
Prev 30417 / 398513 Next

Transforming matrix

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