Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: can range(data.frame) exclude infinite values?

Tags:

range

r

infinity

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?

like image 675
sds Avatar asked May 21 '15 19:05

sds


People also ask

How do I remove an INF from a column in R?

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.

How do you handle infinity in R?

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.

What does the Range function do in R?

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.

How do I exclude missing values in R?

Firstly, we use brackets with complete. cases() function to exclude missing values in R. Secondly, we omit missing values with na. omit() function.


2 Answers

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
like image 27
Henry Avatar answered Oct 04 '22 07:10

Henry


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
like image 80
BrodieG Avatar answered Oct 04 '22 06:10

BrodieG