I am trying to find the range
of a data frame with infinite values:
> f <- data.frame(x=c(1,2),y=c(3,Inf))
> range(f)
[1] 1 Inf
> range(f,finite=TRUE)
Error in FUN(X[[2L]], ...) :
only defined on a data frame with all numeric variables
Calls: Summary.data.frame -> lapply -> FUN
> range(f$y)
[1] 3 Inf
> range(f$y,finite=TRUE)
[1] 3 3
Why am I getting the error?
Can I do better than
> do.call(range,lapply(f,range,finite=TRUE))
[1] 1 3
Is this a bug? Is it known? Should I report it?
You need to use (as David points out in comments):
range.default(f, finite=TRUE)
# [1] 1 3
or
range(f, finite=1)
# [1] 1 3
The function is erroneously requiring finite
to be numeric, but then properly uses it in removing infinite values. Notice:
f2 <- data.frame(x=1:2, y=3:4)
range(f2, finite=TRUE) # Error
Clearly something funny is happening with the generic being a primitive and your argument being an object, likely related to (from ?range
):
range is a generic function: methods can be defined for it directly or via the Summary group generic. For this to work properly, the arguments ... should be unnamed, and dispatch is on the first argument.
So basically, when checking its arguments, it thinks finite=TRUE
is part of the data to check the range on, and since it is a logical it fails the test for numericness. That said, once it gets past that check it computes properly.
To confirm:
range(f, finite=2000)
# [1] 1 3