Less or equal for floats in R
Assuming I want test if 'a' is less or equal than 'b' when both are floats. I would go with
isTRUE(all.equal(a,b)) || a <= b
I'm wondering if there's a better way than this. Is there a function like all.equal for less or equal ( / greater than equal ) that allows "near equality"?
And what should one do if instead of single numbers one wants to compare two vectors of floats?
Update : @shadow pointed to me Numeric comparison difficulty in R
One could of course set tolerance explicitly and avoid all.equal:
tol = 1e-5
# a equals b
abs(a-b) <= tol
# a less or equal to b
a <= b + tol
Here is some discussion about absolute vs. relative tolerance: http://realtimecollisiondetection.net/blog/?p=89
I guess as always, there's no 'right' way to implement this.
Interesting question. I am sure there are better ways, but this simple function takes two vectors of double
s and returns if they are nearly equal element-wise ( mode = "ae"
), given the specified tolerance. It also can return if they are less than ( mode = "lt"
) or if they are nearly equal or less than ( mode = "ne.lt"
), along with their "gt"
equivalents...
near_equal <- function( x , y , tol = 1.5e-8 , mode = "ae" ){
ae <- mapply( function(x,y) isTRUE( all.equal( x , y , tolerance = tol ) ) , x , y )
gt <- x > y
lt <- x < y
if( mode == "ae" )
return( ae )
if( mode == "gt" )
return( gt )
if( mode == "lt" )
return( lt )
if( mode == "ne.gt" )
return( ae | gt )
if( mode == "ne.lt" )
return( ae | lt )
}
# And in action....
set.seed(1)
x <- 1:5
# [1] 1 2 3 4 5
y <- 1:5 + rnorm(5,sd=0.1)
# [1] 0.9373546 2.0183643 2.9164371 4.1595281 5.0329508
near_equal( x , y , tol = 0.05 , mode = "ae" )
#[1] FALSE TRUE TRUE TRUE TRUE
near_equal( x , y , tol = 0.05 , mode = "ne.gt" )
#[1] TRUE TRUE TRUE TRUE TRUE
Hope that helps.
链接地址: http://www.djcxy.com/p/58536.html下一篇: 在R中花车少或相等