假设当两个都是浮点数时,我想要测试'a'是否小于或等于'b'。 我会去
isTRUE(all.equal(a,b)) || a <= b我想知道是否有比这更好的方法。 是否存在像all.equal这样的函数,用于更小或相等(/大于等于)允许“接近相等”?
如果不是单个数字,人们想要比较浮动的两个向量,应该怎么做?
更新 :@shadow指向我R中的数字比较难度
当然可以明确地设置容差并避免all.equal:
tol = 1e-5 # a equals b abs(a-b) <= tol # a less or equal to b a <= b + tol以下是关于绝对与相对容忍度的一些讨论: http : //realtimecollisiondetection.net/blog/? p = 89
我想一如既往,没有“正确”的方式来实现这一点。
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 <= bI'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 + tolHere 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.
最满意答案
有趣的问题。 我确信有更好的方法,但是这个简单的函数需要两个double的向量,并且如果它们在元素方面( mode = "ae" )几乎相等则返回,给定指定的容差。 它还可以返回,如果它们小于( mode = "lt" )或者它们几乎等于或小于( mode = "ne.lt" ),以及它们的"gt"等价物......
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希望有所帮助。
Interesting question. I am sure there are better ways, but this simple function takes two vectors of doubles 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 TRUEHope that helps.
R中的浮点数小于或等于(Less or equal for floats in R)假设当两个都是浮点数时,我想要测试'a'是否小于或等于'b'。 我会去
isTRUE(all.equal(a,b)) || a <= b我想知道是否有比这更好的方法。 是否存在像all.equal这样的函数,用于更小或相等(/大于等于)允许“接近相等”?
如果不是单个数字,人们想要比较浮动的两个向量,应该怎么做?
更新 :@shadow指向我R中的数字比较难度
当然可以明确地设置容差并避免all.equal:
tol = 1e-5 # a equals b abs(a-b) <= tol # a less or equal to b a <= b + tol以下是关于绝对与相对容忍度的一些讨论: http : //realtimecollisiondetection.net/blog/? p = 89
我想一如既往,没有“正确”的方式来实现这一点。
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 <= bI'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 + tolHere 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.
最满意答案
有趣的问题。 我确信有更好的方法,但是这个简单的函数需要两个double的向量,并且如果它们在元素方面( mode = "ae" )几乎相等则返回,给定指定的容差。 它还可以返回,如果它们小于( mode = "lt" )或者它们几乎等于或小于( mode = "ne.lt" ),以及它们的"gt"等价物......
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希望有所帮助。
Interesting question. I am sure there are better ways, but this simple function takes two vectors of doubles 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 TRUEHope that helps.
发布评论