Function to identify problematic datatypes

Posted by Zach on Stack Overflow See other posts from Stack Overflow or by Zach
Published on 2012-10-24T22:02:43Z Indexed on 2012/10/24 23:01 UTC
Read the original article Hit count: 184

Filed under:

I just spent several hours debugging some R code, only to discover that the error was due to an Inf that had sneaked in during my calculations. I had checked for NA, but hadn't thought to check for Inf.

I wrote the following function to help prevent this situation in the future:

is.bad <- function(x){
  is.na(x) | is.nan(x) | is.infinite(x)
}

>     is.bad(c(NA, NaN, Inf, -Inf, 0, 1, 1000, 1e6))
[1]  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE

Are there any other special data types in R I should be aware of?

© Stack Overflow or respective owner

Related posts about r