Are there any written functions in R that allow for a margin of error when comparing numbers or vectors? x==y would evaluate to TRUE if the two values were within a specified tolerable error margin.
Example if the target value is 77 and my allowable margin of error was 5%, asking the question does x==77? would evaluate to TRUE if x is within a 5% margin of error around 77, i.e. any values of x between 73.15 and 80.85 would evaluate to TRUE.
I can right a function to do the "==" comparison but couldn't come up with a solution that builds an "acceptance" range around each of the target values so that if my allowable margin of error was 5%, any of the dta elements that falls within +/- 5% of tgt values would evaluate to TRUE.
tgt <- c(45,77,92)
dta <- c(33,41,44,60,68,71,77,78,87,95)
sapply(tgt, function(i) i==dta)
This compares each of tgt to each of dta. The only comparison that returns TRUE is when 77 is compared to 77. I would have to build a range around each of tgt elements then compare each of dta elements to that range returning true whenever any of dta elements falls inside any of the ranges created. As I started building the ranges I got so confused. The result I am looking for would evaluate to true when 44 is compared to 45 as it's short by less than 5%, similarly 78 is close enough to 77 to evaluate to true as well as 95 to 92.