Try to search your question here, if you can't find : Ask Any Question Now ?

R Writing a function input equal integer,but input maybe NA

HomeCategory: stackoverflowR Writing a function input equal integer,but input maybe NA
john asked 2 weeks ago

I want to write a function,Determine the input is equal to 1,
but The input maybe includes (NA NULL).

> equals1 <- function(x){
+   return(x==1)
+ }

> equals1(1)
[1] TRUE
> equals1(Inf)
[1] FALSE
> equals1(NA)
[1] NA
> equals1(NULL)
logical(0)
> equals1(NaN)
[1] NA

I want is as follows´╝Ü

equals1<-function(x){
   if(is.null(x)){
     return(FALSE)
     }else if(is.na(x)){
       return(FALSE)
     }else{return(x==1)}
}
c(equals1(1),equals1(Inf),equals1(NA),equals1(NULL),equals1(NaN))
# [1]  TRUE FALSE FALSE FALSE FALSE

Is there a simple function?
Thanks!

1 Answers
Best Answer
Matthias answered 2 weeks ago
Your Answer

3 + 13 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com