[R] generic handling of NA and NaN and NULL

Spencer Graves spencer.graves at pdf.com
Fri Feb 14 00:52:03 CET 2003


How about the following modification:

 > f3 <- function(n){
+   if(length(n)<1)return(FALSE)
+   n.pi <- (abs(n)<pi)
+   n.pi[is.na(n.pi)] <- FALSE
+   n.pi
+ }
 >
 >  x <- 1:10
 > f3(x[x>11])
[1] FALSE

Spencer

Liaw, Andy wrote:
> The version I gave is obviously not vectorized (since your original version
> seem to indicate that the argument won't have length > 1, otherwise the if()
> won't really make sense).
> 
> Replacing is.null(n) with length(n)!=1 (or length(n)==0) should do the trick
> (I hope!).
> 
> Andy
> 
> 
>>-----Original Message-----
>>From: Robin Hankin [mailto:r.hankin at auckland.ac.nz]
>>Sent: Thursday, February 13, 2003 6:30 PM
>>To: andy_liaw at merck.com
>>Cc: r.hankin at auckland.ac.nz; r-help at stat.math.ethz.ch
>>Subject: Re: [R] generic handling of NA and NaN and NULL
>>
>>
>>
>>
>>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
>>
> 
> 
> 
> ------------------------------------------------------------------------------
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> http://www.stat.math.ethz.ch/mailman/listinfo/r-help




More information about the R-help mailing list