Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check a data.frame for any non-finite

Tags:

r

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 ) ) )
like image 316
SFun28 Avatar asked Nov 17 '11 19:11

SFun28


2 Answers

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)))
}
like image 125
joran Avatar answered Oct 26 '22 23:10

joran


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) )
like image 36
JD Long Avatar answered Oct 26 '22 22:10

JD Long