Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A function that returns TRUE on NA, NULL, NaN in R?

Tags:

r

Is there a function in R that behaves like this:

isnothing = function(x) {
    is.null(x) | is.na(x) | is.nan(x)
}
like image 673
qed Avatar asked Oct 29 '13 10:10

qed


People also ask

Is NaN the same as Na in R?

In R, missing values are represented by the symbol NA (not available). Impossible values (e.g., dividing by zero) are represented by the symbol NaN (not a number).

Is it null or Na R?

In simple words, NULL represents the null or an empty object in R. NA represents a missing value in R. NA can be updated in R by vectors, list and other R objects whereas NULL cannot be coerced. NA elements can be accessed and be managed using various na functions such as is.na(), na.

Why does R return NaN?

NaN in R means “Not a Number” which means there is something or some value, but it cannot be described in the computer. NaN designates a result that cannot be calculated for whatever reason, or it is not a floating-point number. If you divide 0 / 0, then it will return NaN.

What is the difference between Null Na NaN and INF values in R?

NA and “NA” (as presented as string) are not interchangeable. NA stands for Not Available. NaN stands for Not A Number and is a logical vector of a length 1 and applies to numerical values, as well as real and imaginary parts of complex values, but not to values of integer vector. NaN is a reserved word.


3 Answers

I was also missing such a function and added this to my .Rprofile ages ago. If someone knows of a base function that does the same thing I also want to know.

is.blank <- function(x, false.triggers=FALSE){
    if(is.function(x)) return(FALSE) # Some of the tests below trigger
                                     # warnings when used on functions
    return(
        is.null(x) ||                # Actually this line is unnecessary since
        length(x) == 0 ||            # length(NULL) = 0, but I like to be clear
        all(is.na(x)) ||
        all(x=="") ||
        (false.triggers && all(!x))
    )
}

As @shadow mentioned, NA, NaN and NULL have different meanings that are important to understand. However, I find this function useful when I make functions containing optional arguments with default values, that I want to allow the user to suppress by setting them to any "undefined" value.

One such example is xlab of plot. I can never remember if it is xlab=NA, xlab=FALSE, xlab=NULL or xlab="". Some produce the desired result and some don't, so I found it convenient to catch all with the above function when I develop code, particularly if other people will use it too.

like image 84
Backlin Avatar answered Oct 24 '22 05:10

Backlin


I believe you are basically looking for what gtools invalid() function does.

 ?gtools::invalid

For example,

gtools::invalid(NA)
[1] TRUE
gtools::invalid(NULL)
[1] TRUE
gtools::invalid(NaN)
[1] TRUE
like image 45
giocomai Avatar answered Oct 24 '22 04:10

giocomai


I had a very similar problem too. I was trying to check whether row names of a given matrix or data frame are proper.

x <- matrix(1:6,ncol=2)
isnothing = function(x) {
  any(is.null(x))  | any(is.na(x))  | any(is.nan(x)) 
}
isnothing(rownames(x))

The function will throw an error. But when I used short circuiting and changed it to:

isnothing = function(x) {
  any(is.null(x))  || any(is.na(x))  || any(is.nan(x)) 
}
isnothing(rownames(x))

It solved my problem. I guess checking first nullity and then going forward to check other cases if it is FALSE solved my issue. I checked it with couple problematic cases that I can think of and it worked. I don't know whether there are exceptions to it but it worked for my purposes for now.

rownames(x) <- c("a",NaN,NA)
isnothing(rownames(x))
like image 42
HBat Avatar answered Oct 24 '22 05:10

HBat