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

前端 未结 4 970
渐次进展
渐次进展 2021-01-01 20:57

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.f

4条回答
  •  鱼传尺愫
    2021-01-01 21:35

    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) )
    

提交回复
热议问题