Hello. I have a two statement logical that if NA is returned for the second statement I want to rely on result of the first statement. I still would like to use both when I can though. x <- c(1:5) y <- c(1,2,NA,4,5) x < 5 & x-y == 0 How can I trick R to refer back to (x < 5) where it is NA on the third value? Thanks. [[alternative HTML version deleted]]
On 12-12-10 7:55 PM, Hans Thompson wrote:> Hello. > > I have a two statement logical that if NA is returned for the second > statement I want to rely on result of the first statement. I still would > like to use both when I can though. > > x <- c(1:5) > y <- c(1,2,NA,4,5) > x < 5 & x-y == 0 > > How can I trick R to refer back to (x < 5) where it is NA on the third > value?Use is.na() to test for NA. I think this does what you want: x < 5 & (is.na(x-y == 0) | x-y == 0) Duncan Murdoch
On Dec 10, 2012, at 4:55 PM, Hans Thompson wrote:> Hello. > > I have a two statement logical that if NA is returned for the second > statement I want to rely on result of the first statement. I still > would > like to use both when I can though. > > x <- c(1:5) > y <- c(1,2,NA,4,5) > x < 5 & x-y == 0 > > How can I trick R to refer back to (x < 5) where it is NA on the third > value?If you program so that you "trick" your tool, you will fail to progress in understanding it> Since NA | TRUE returns TRUE (and is clearly documented as such) this is not trickery: > x < 5 & (is.na(y) | x-y == 0) [1] TRUE TRUE TRUE TRUE FALSE -- David Winsemius, MD Alameda, CA, USA