Reputation: 30301
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?
Upvotes: 1
Views: 71
Reputation: 226097
!is.finite(x)
is equivalent to your is.bad(x)
; it detects Inf
(and -Inf
), NA
, and NaN
values.
Upvotes: 4
Reputation: 17412
It depends a bit on what you're trying to do. I had a similar problem recently when I was creating a linear model for a bunch of data subsets. What worked for me to get coefficients when they were possible, and an NA instead of a program stopping error message was:
Coef <- ifelse(is.numeric(try(coef(lm(y~x, data=DF)), silent=T)), coef(lm(y~x+0, DF)), NA)
So I would get a vector
[1] 2.3 4.3 5.4 6.2 2.8 NA 3.2
Properly spaced, instead of an error message.
Upvotes: 1
Reputation: 8267
Depending on what you are doing, is.null()
could possibly at some point also save you a lot of time. Hard to say in general.
Upvotes: 1