Re: [R] transforming a matrix of logical to 0 and 1 while keepin

2011-07-20 Thread Ted Harding
It is easier and more straightforward than any of the suggestions so far. Simply multiply the matrix by 1, or add 0 to it: x<-matrix(c(TRUE,FALSE,TRUE,FALSE,TRUE,FALSE,TRUE,FALSE,TRUE),nrow=3) 1*x # [,1] [,2] [,3] # [1,]101 # [2,]010 # [3,]101

Re: [R] transforming a matrix of logical to 0 and 1 while keeping the dim of matrix

2011-07-20 Thread Joshua Wiley
Hi, I am not sure about "correct", but R stores logical values TRUE/FALSE as 1/0 already so simply changing the mode would suffice: mode(x) <- "numeric" alternately x + 0 HTH, Josh On Wed, Jul 20, 2011 at 8:16 AM, Julian TszKin Chan wrote: > Hi all, > > Suppose I have a matrix of logical va

Re: [R] transforming a matrix of logical to 0 and 1 while keeping the dim of matrix

2011-07-20 Thread David Winsemius
Two further methods: > x+0 [,1] [,2] [,3] [1,]101 [2,]010 [3,]101 > mode(x)<- "numeric"; x [,1] [,2] [,3] [1,]101 [2,]010 [3,]101 On Jul 20, 2011, at 11:27 AM, Sarah Goslee wrote: How about this: x<-matrix(c(T

Re: [R] transforming a matrix of logical to 0 and 1 while keeping the dim of matrix

2011-07-20 Thread Sarah Goslee
How about this: > x<-matrix(c(TRUE,FALSE,TRUE,FALSE,TRUE,FALSE,TRUE,FALSE,TRUE),nrow=3) > x [,1] [,2] [,3] [1,] TRUE FALSE TRUE [2,] FALSE TRUE FALSE [3,] TRUE FALSE TRUE > ifelse(x, 1, 0) [,1] [,2] [,3] [1,]101 [2,]010 [3,]101 Sarah On Wed, J

[R] transforming a matrix of logical to 0 and 1 while keeping the dim of matrix

2011-07-20 Thread Julian TszKin Chan
Hi all, Suppose I have a matrix of logical value: x<-matrix(c(TRUE,FALSE,TRUE,FALSE,TRUE,FALSE,TRUE,FALSE,TRUE),nrow=3) I would like to change the value of FALSE to 0 and TRUE to 1. An obvious way to do it is : y<-as.numeric(x) However this method doesn't keep the dim of x. I also need to copy

Re: [R] transforming a matrix

2009-03-13 Thread David Winsemius
Am I correct in thinking that you actually want a function that will take the three row matrix Ystart <- matrix(c(1:3, 34,-56,27), ncol=2) and return the 4 column matrix offered? As it is I see no way that we could know what rule you wanted to apply to that four row X matrix to get the 9

[R] transforming a matrix

2009-03-13 Thread Dimitri Szerman
Hello, I have a matrix such as > X = matrix(c(1:4,29,32,56,-45), ncol=2) and I want one like > Y=matrix(c( 1 , 34 , 1 , 34 , 2 , -56 , 1 , 34 , 3 , 27 , 1 , 34 , 1 , 34 , 2 , -56 , 2 , -56 , 2 , -56 , 3 , 27 , 2 , -56 , 1 , 34 , 3 , 27 , 2 , -56 , 3 , 27 , 3 , 27 , 3 , 27 ), ncol=4, byrow=T) Ho