My goal is to check if a given number is in a cut
type interval or range like this: "[1.86e+03,2.43e+03]". I get the interval as a string.
I'm having trouble with the higher end of the interval for some reason and can't see what I'm doing wrong, the lower seems to work fine and I haven't included it in the code below.
Here's a part of the code that doesn't seem to work:
library(readr)
IsNumLess <- function(num, interval) {
intervalL <- unlist(strsplit(interval, ",")) #split on comma
lastSt <- intervalL[2] #get the whole second part
lastNum <- parse_number(lastSt) #get just the number, without ) or ]
if (endsWith(lastSt, ']')) #up to and including
{
if (!(num <= lastNum))
{
print(num)
print(lastNum)
print(num <= lastNum) #this and line below should return the same value
print(2430 <= 2430)
print("f3")
return(FALSE)
}
}
else # ) - up to but not including
{
if (!(num < lastNum))
{
print("f4")
return(FALSE)
}
}
return(TRUE)
}
If I run this IsNumLess(2430, "[1.86e+03,2.43e+03]")
it comes back as FALSE, and it should be TRUE as 2430 <= 2.43e+03...
> IsNumLessMin(2430, "[1.86e+03,2.43e+03]")
[1] 2430
[1] 2430
[1] FALSE
[1] TRUE
[1] "f3"
[1] FALSE
Edit: Thank you G5W, the duplicate question link got me to where I needed.... This line worked in the second 'if':
if (!(num < lastNum | isTRUE(all.equal(num, lastNum))))