Quantcast

Changing a logical matrix into a numeric matrix

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Changing a logical matrix into a numeric matrix

emj83
Hi,

I would like to turn my TRUE/FALSE matrix into a 1/0 matrix (i.e. True=1 and False=0)

      [,1]  [,2]  [,3]  
[1,]  TRUE FALSE FALSE
[2,]  TRUE  TRUE FALSE
[3,]  TRUE  TRUE  TRUE

      [,1]  [,2]  [,3]  
[1,]    1    0     0
[2,]    1    1     0
[3,]    1    1     1

Is there a quick way of doing this without a loop?

Thanks Emma
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Changing a logical matrix into a numeric matrix

Søren Højsgaard
Just multiply by 1:

> m <- matrix(c(T,T,F,T),nr=2)
> m
     [,1]  [,2]
[1,] TRUE FALSE
[2,] TRUE  TRUE
> m*1
     [,1] [,2]
[1,]    1    0
[2,]    1    1





-----Oprindelig meddelelse-----
Fra: [hidden email] [mailto:[hidden email]] På vegne af emj83
Sendt: 10. januar 2011 10:17
Til: [hidden email]
Emne: [R] Changing a logical matrix into a numeric matrix


Hi,

I would like to turn my TRUE/FALSE matrix into a 1/0 matrix (i.e. True=1 and
False=0)

      [,1]  [,2]  [,3]  
[1,]  TRUE FALSE FALSE
[2,]  TRUE  TRUE FALSE
[3,]  TRUE  TRUE  TRUE

      [,1]  [,2]  [,3]  
[1,]    1    0     0
[2,]    1    1     0
[3,]    1    1     1

Is there a quick way of doing this without a loop?

Thanks Emma
--
View this message in context: http://r.789695.n4.nabble.com/Changing-a-logical-matrix-into-a-numeric-matrix-tp3206797p3206797.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Changing a logical matrix into a numeric matrix

Joshua Wiley-2
In reply to this post by emj83
Hi Emma,

The easiest way I know uses a bit of a trick. If your matrix is named 'X'

Then: X + 0 will convert it to numeric data.

HTH,

Josh

On Jan 10, 2011, at 1:17, emj83 <[hidden email]> wrote:

>
> Hi,
>
> I would like to turn my TRUE/FALSE matrix into a 1/0 matrix (i.e. True=1 and
> False=0)
>
>      [,1]  [,2]  [,3]  
> [1,]  TRUE FALSE FALSE
> [2,]  TRUE  TRUE FALSE
> [3,]  TRUE  TRUE  TRUE
>
>      [,1]  [,2]  [,3]  
> [1,]    1    0     0
> [2,]    1    1     0
> [3,]    1    1     1
>
> Is there a quick way of doing this without a loop?
>
> Thanks Emma
> --
> View this message in context: http://r.789695.n4.nabble.com/Changing-a-logical-matrix-into-a-numeric-matrix-tp3206797p3206797.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Changing a logical matrix into a numeric matrix

ted.harding-3
In reply to this post by emj83
On 10-Jan-11 09:17:26, emj83 wrote:

> Hi,
>
> I would like to turn my TRUE/FALSE matrix into a 1/0 matrix (i.e.
> True=1 and
> False=0)
>
>       [,1]  [,2]  [,3]  
> [1,]  TRUE FALSE FALSE
> [2,]  TRUE  TRUE FALSE
> [3,]  TRUE  TRUE  TRUE
>
>       [,1]  [,2]  [,3]  
> [1,]    1    0     0
> [2,]    1    1     0
> [3,]    1    1     1
>
> Is there a quick way of doing this without a loop?
> Thanks Emma

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) <[hidden email]>
Fax-to-email: +44 (0)870 094 0861
Date: 10-Jan-11                                       Time: 09:39:15
------------------------------ XFMail ------------------------------

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Changing a logical matrix into a numeric matrix

emj83
Thanks for everyone's suggestions....I didn't realise it was so straightforward- thanks for teaching me new tricks!

Emma
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Changing a logical matrix into a numeric matrix

baptiste auguie-5
In reply to this post by emj83
Hi,

Try this,

mode(m) <- "integer"

HTH,

baptiste

On 10 January 2011 10:17, emj83 <[hidden email]> wrote:

>
> Hi,
>
> I would like to turn my TRUE/FALSE matrix into a 1/0 matrix (i.e. True=1 and
> False=0)
>
>      [,1]  [,2]  [,3]
> [1,]  TRUE FALSE FALSE
> [2,]  TRUE  TRUE FALSE
> [3,]  TRUE  TRUE  TRUE
>
>      [,1]  [,2]  [,3]
> [1,]    1    0     0
> [2,]    1    1     0
> [3,]    1    1     1
>
> Is there a quick way of doing this without a loop?
>
> Thanks Emma
> --
> View this message in context: http://r.789695.n4.nabble.com/Changing-a-logical-matrix-into-a-numeric-matrix-tp3206797p3206797.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Changing a logical matrix into a numeric matrix

djhurio
Nice solution :)

Another one:

m <- matrix(c(T,T,F,T),nr=2)
m
matrix(as.integer(m), dim(m))
--
Mārtiņš Liberts
Mathematician of Statistics at Central Statistical Bureau of Latvia
Loading...