Testing numeric variables for NA/NaN/Inf

April 9, 2019
By

[This article was first published on R – Statistical Odds & Ends, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In R, a numeric variable is either a number (like 0, 42, or -3.14), or one of 4 special values: NA, NaN, Inf or -Inf. It can be hard to remember how the is.x functions treat each of the special values, especially NA and NaN! The table below summarizes how each of these values is treated by different base R functions. Functions are listed in alphabetical order.

Function Typical number NA NaN Inf -Inf
is.double TRUE FALSE TRUE TRUE TRUE
is.finite TRUE FALSE FALSE FALSE FALSE
is.infinite FALSE FALSE FALSE TRUE TRUE
is.integer *1 FALSE FALSE FALSE FALSE
is.na FALSE TRUE TRUE FALSE FALSE
is.nan FALSE FALSE TRUE FALSE FALSE
is.null FALSE FALSE FALSE FALSE FALSE
is.numeric TRUE *2 TRUE TRUE TRUE

*1: is.integer can return TRUE or FALSE, depending on whether the number is an integer or not.

*2: R actually has different types of NAs (see here for more details). is.numeric(NA) returns FALSE, but is.numeric(NA_integer_) and is.numeric(NA_real_) return TRUE. Interestingly, is.numeric(NA_complex_) returns FALSE.

To leave a comment for the author, please follow the link and comment on their blog: R – Statistical Odds & Ends.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)