[R] generic handling of NA and NaN and NULL
Liaw, Andy
andy_liaw at merck.com
Fri Feb 14 00:40:05 CET 2003
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
>
------------------------------------------------------------------------------
More information about the R-help
mailing list