Peter Flom
Peter Flom

Reputation: 2416

Evaluation of values and an OR statement in R

As part of an R program I create matrices patients, invited and missinrow and scalars numdrop and maxses. These all exist and are properly created.

I then try this line

else if (invited[iPatient, iSession] >= maxses |
  missinrow[iPatient,iSession] >= (numdrop+1 )) {
    patients[iPatient,iSession] <- 'D'}   

The part before the | works perfectly, the part after the | doesn't seem to do anything at all.

I've tried making this line into two statements, I've tried || instead of |, I've stared at it for hours.
Help! Thanks

Added 6/28/11 The program reads parameters from another file and creates data. But here is some output:

invited[1:5, 1:14] 

gives

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
[1,]    0    1    2    3    4    5    6    7    8     9    10    11    12    12
[2,]    0    1    2    3    4    5    6    7    8     9    10    11    12    12
[3,]    0    1    2    3    4    5    6    7    8     9    10    11    12    12
[4,]    0    0    0    1    2    3    4    5    6     7     8     9    10    11
[5,]    0    0    0    1    2    3    4    5    6     7     8     9    10    11

which is fine

missinrow[1:5, 1:14]

gives

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
[1,]    1    2    3    4    5    6    7    8    9    10     0     0     0     0
[2,]    1    2    3    4    5    6    7    8    9    10     0     0     0     0
[3,]    1    2    3    4    5    0    0    0    0     0     0     1     0     0
[4,]    0    0    1    2    0    0    1    2    3     4     5     6     7     0
[5,]    0    0    1    2    3    4    5    6    7     8     9    10    11    12

also fine, but

patients[1:5, 1:14]

gives

    [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
[1,] "S"  "S"  "S"  "S"  "S"  "S"  "S"  "S"  "S"  "S"   "A"   "S"   "D"   "D"  
[2,] "S"  "S"  "S"  "S"  "S"  "S"  "S"  "S"  "S"  "S"   "A"   "A"   "D"   "D"  
[3,] "S"  "S"  "S"  "S"  "S"  "A"  "S"  "A"  "A"  "A"   "S"   "S"   "D"   "D"  
[4,] "NA" "W"  "S"  "S"  "A"  "S"  "S"  "S"  "S"  "S"   "S"   "S"   "S"   "A"  
[5,] "NA" "W"  "S"  "S"  "S"  "S"  "S"  "S"  "S"  "S"   "S"   "S"   "S"   "S"  

and we see that the cells where invited is higher than maxses (which is 12) are all D, as they should be, but those where missinrow is higher than numdrop + 1 (which is 4 +1) are not D.

Upvotes: 2

Views: 2819

Answers (1)

Henry
Henry

Reputation: 6784

Here is some code including your expression

invited   <- 10 * matrix(12:1, nrow=3)
missinrow <- matrix(1:12, nrow=3)
patients  <- matrix(rep("A",12), nrow=3)
maxses    <- 100
numdrop   <- 7 

for (iPatient in 1:3) { 
    for (iSession in 1:4) {
        if (iPatient==iSession) { patients[iPatient,iSession] <- 'B'}
        else if (invited[iPatient, iSession] >= maxses |
          missinrow[iPatient,iSession] >= (numdrop+1 )) {
            patients[iPatient,iSession] <- 'D'} 
                          }
                      }

Looking at the three matrices

> invited
     [,1] [,2] [,3] [,4]
[1,]  120   90   60   30
[2,]  110   80   50   20
[3,]  100   70   40   10
> missinrow
     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12
> patients
     [,1] [,2] [,3] [,4]
[1,] "B"  "A"  "A"  "D" 
[2,] "D"  "B"  "D"  "D" 
[3,] "D"  "A"  "B"  "D" 

I would say the else if statement was working as expected, and that the "D"s on the left hand side of patients corresponded to the second else if condition.

Upvotes: 1

Related Questions