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:[EMAIL PROTECTED]]
Sent: Thursday, February 13, 2003 6:30 PM
To: [EMAIL PROTECTED]
Cc: [EMAIL PROTECTED]; [EMAIL PROTECTED]
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
[EMAIL PROTECTED]
tel 0064-9-373-7599 x6820; FAX 0064-9-373-7042
------------------------------------------------------------------------------
______________________________________________
[EMAIL PROTECTED] mailing list
http://www.stat.math.ethz.ch/mailman/listinfo/r-help
______________________________________________
[EMAIL PROTECTED] mailing list
http://www.stat.math.ethz.ch/mailman/listinfo/r-help