I'd like to check if a data.frame has any non-finite elements.
This seems to evaluate each column, returning FALSE for each (I'm guessing its evaluating the data.frame as a list):
any( !is.finite( x ) )
I don't understand why this behaves differently from the above, but it works fine if just checking for NAs:
any( !is.na( x ) )
I'd like the solution to be as efficient as possible. I realize I can just do...
any( !is.finite( as.matrix( x ) ) )
If you type methods(is.na)
you'll see that it has a data.frame
method, which probably explains why it works the way you expect, where is.finite
does not. The usual solution would be to write one yourself, since it's only one line. Something like this maybe,
is.finite.data.frame <- function(obj){
sapply(obj,FUN = function(x) all(is.finite(x)))
}
I'm assuming the error you are getting is the following:
> any( is.infinite( z ) )
Error in is.infinite(z) : default method not implemented for type 'list'
This error is because the is.infinite()
and the is.finite()
functions are not implemented with a method for data.frames. The is.na()
function does have a data.frame method.
The way to work around this is to apply()
the function to every row, column, or element in the data.frame. Here's an example using sapply()
to apply the is.infinite()
function to each element:
x <- c(1:10, NA)
y <- c(1:11)
z <- data.frame(x,y)
any( sapply(z, is.infinite) )
## or
any( ! sapply(z, is.finite) )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With