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?
replace([np. inf, -np. inf], 0, inplace=True)” is used and this will replace all negative and positive infinite value with zero in “Marks” column of Pandas dataframe.
Some output values tend to have infinite value as the result e.g after diving by zero. To deal with such infinite values, we use is. infinite () A is. infinite () function finds infinite values in the given vector and returns TRUE value for them.
Range in R returns a vector that contains the minimum and maximum values of the given argument - known in statistics as a range. You can think of range as an interval between the lowest and the highest value within the vector.
Firstly, we use brackets with complete. cases() function to exclude missing values in R. Secondly, we omit missing values with na. omit() function.
There are probably multiple possibilities. If everything is numeric then one is
> f <- data.frame(x=c(1,2),y=c(3,Inf))
> range(as.matrix(f),finite=TRUE)
[1] 1 3
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
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