Skip to content
Prev 246901 / 398506 Next

Changing a logical matrix into a numeric matrix

On 10-Jan-11 09:17:26, emj83 wrote:
As soon as logic encounters arithmetic, TRUTH and FALSITY
cease to exist -- they get hijacked!

Hence: multiply your matrix by 1. For example:

  A <- matrix(c(TRUE,FALSE,TRUE,TRUE,TRUE,FALSE,FALSE,TRUE),ncol=4)
  A
  #       [,1] [,2]  [,3]  [,4]
  # [1,]  TRUE TRUE  TRUE FALSE
  # [2,] FALSE TRUE FALSE  TRUE

  B <- 1*A
  B
  #      [,1] [,2] [,3] [,4]
  # [1,]    1    1    1    0
  # [2,]    0    1    0    1

(You could also add zero: B <- 0+A)

Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <ted.harding at wlandres.net>
Fax-to-email: +44 (0)870 094 0861
Date: 10-Jan-11                                       Time: 09:39:15
------------------------------ XFMail ------------------------------