[R] Logical matrices

Spencer Graves spencer.graves at pdf.com
Wed Nov 5 18:44:35 CET 2003


      Please read 'help("|")':  "|" performs elementwise comparison, 
while "||" 'evaluates left to right examining only the first element of 
each vector.  Evaluation proceeds only until the result is determined.' 

      Consider the following: 

 > B <- cbind(diag(3), rep(0, 3), c(0,0,1))
 > B[1,3] <- 1
 > B[1,]|B[3,]
[1]  TRUE FALSE  TRUE FALSE  TRUE
 > B[1,]||B[3,]
[1] TRUE

      hope this helps.  spencer graves

Aurora Torrente wrote:

> Hello,
> I've been trying to work with 0-1 matrices as if they were logical, 
> but using the logical operators doesn't produce what I need, for using 
> the matrix B:
>
>     [,1] [,2] [,3] [,4] [,5]
> [1,]    1    0    1    0    0
> [2,]    0    1    0    0    0
> [3,]    0    0    1    0    1
>
> gives me the following:
>
> > B[1,]<-B[1,] || B[3,]
>
> > B
>     [,1] [,2] [,3] [,4] [,5]
> [1,]    1    1    1    1    1
> [2,]    0    1    0    0    0
> [3,]    0    0    1    0    1
>
> instead of :
>
>     [,1] [,2] [,3] [,4] [,5]
> [1,]    1    0    1    0    1
> [2,]    0    1    0    0    0
> [3,]    0    0    1    0    1
>
> which is what I need.
> I've tried to convert it into a logical matrix, but the result was a 
> vector:
>
> > C<-as.logical(B)
> > C
> [1]  TRUE FALSE FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE  TRUE FALSE 
> FALSE  TRUE FALSE  TRUE
>
> What could I do? Thanks for your help,
>
>        Aurora
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help




More information about the R-help mailing list