, < >, >=, <= = ( ==).
areTherePossibleValues = function(condition = "x<1 & x>2", tolerance = 1e-10){
condition = paste(condition, collapse = "&")
condition = tolower(condition)
condition = gsub("[ ,x]","",condition)
condition = gsub(">=","g",condition)
condition = gsub("<=","s",condition)
condition = gsub("[==,=]","e",condition)
condition = unlist(strsplit(condition,"&"))
Upper = rep(x = NA, times = length(condition))
Lower = rep(x = NA, times = length(condition))
for (i in 1:length(condition)){
number = as.numeric(gsub(pattern = "[<,>,e,g,s]", replacement = "", condition[i]))
comparator = substr(condition[i], start = 1, stop = 1)
if (comparator == ">"){
Lower[i] = number + tolerance
} else if (comparator == "<"){
Upper[i] = number - tolerance
} else if (comparator == "g"){
Lower[i] = number
} else if (comparator == "s"){
Upper[i] = number
} else if (comparator == "e"){
Upper[i] = number
Lower[i] = number
}
}
Upper = as.numeric(Upper[which(is.na(Upper) == FALSE)])
Lower = as.numeric(Lower[which(is.na(Lower) == FALSE)])
if (length(Upper) == 0 & length(Lower) > 0){
ans = TRUE
} else if (length(Lower) == 0 & length(Upper) > 0){
ans = TRUE
} else {
ans = (min(Upper) - max(Lower)) >= 0
}
if (ans == FALSE){
return(ans)
} else {
return(paste(ans," for (",max(Lower)," < x < ",min(Upper),")",sep = ""))
}
}
areTherePossibleValues(">=5 & <50 & >30 & >45")
#[1] "TRUE for (45.0000000001 < x < 49.9999999999)"
areTherePossibleValues("x>5 & x<3")
#[1] FALSE
areTherePossibleValues(c("<5",">=2 & =4"))
#[1] "TRUE for (4 < x < 4)"