[R] generic handling of NA and NaN and NULL

Robin Hankin r.hankin at auckland.ac.nz
Fri Feb 14 00:33:25 CET 2003


Hello Andy

thanks for this; but


R> x <- 1:10
R> f
function(n) {
  if(is.null(n) || is.na(n) || abs(n) < pi) {
    return(FALSE)
  } else {
    return(TRUE)
  }
}
R> x <- 1:10
R> f(x[x>11])
Error in if (is.null(n) || is.na(n) || abs(n) < pi) { : 
	missing value where logical needed
> 






> 
> Try:
> 
> f <- function(n) {
>   if(is.null(n) || is.na(n) || abs(n) < pi) {
>     return(FALSE)
>   } else {
>     return(TRUE)
>   }
> }
> 
> Note that the order of the conditions inside if() matters: is.na(n) only
> gets evaluated if is.null(n) is FALSE, and so on.
> 
> Andy
> 


-- 

Robin Hankin, Lecturer,
School of Geography and Environmental Science
Tamaki Campus
Private Bag 92019 Auckland
New Zealand

r.hankin at auckland.ac.nz
tel 0064-9-373-7599 x6820; FAX 0064-9-373-7042




More information about the R-help mailing list