人眼看不出x值是满足条件的
x<1 & x>2
但我怎样才能让 R 看到它。我想在一个通过比较(比如字符串)而不一定是数据的函数中使用它。假设我想编写一个函数来检查比较的组合是否可以实现,就像这样
areTherePossibleValues <- function(someString){
someCode
}
areTherePossibleValues("x<1 & x>2")
[1] FALSE
我的意思是可以通过解释作为比较符号等的子串来做到这一点,但我觉得必须有更好的方法。 R 比较函数('<'、'>'、'=' 等)本身实际上可能就是这个问题的答案,对吧?
最佳答案
另一种选择是使用库 validatetools
(免责声明,我是它的作者)。
library(validatetools)
rules <- validator( r1 = x < 1, r2 = x > 2)
is_infeasible(rules)
# [1] TRUE
make_feasible(rules)
# Dropping rule(s): "r1"
# Object of class 'validator' with 1 elements:
# r2: x > 2
# Rules are evaluated using locally defined options
# create a set of rules that all must hold:
rules <- validator( x > 1, x < 2, x < 2.5)
is_infeasible(rules)
# [1] FALSE
remove_redundancy(rules)
# Object of class 'validator' with 2 elements:
# V1: x > 1
# V2: x < 2
rules <- validator( x >= 1, x < 1)
is_infeasible(rules)
# [1] TRUE
关于r - 如何预先确定互斥比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41870886/