[R] Problem with <= (less than or equal): not giving the expected result
Rainer Johannes
Johannes.Rainer at eurac.edu
Tue Apr 5 14:46:17 CEST 2016
Thanks Adrian and Thierry (from the previous answer).
I was aware of the all.equal function, but there is nothing similar for <= (e.g. all.smallerEqual)?
cheers, jo
On 05 Apr 2016, at 14:31, Adrian Dușa <dusa.adrian at unibuc.ro<mailto:dusa.adrian at unibuc.ro>> wrote:
Yes, that does have to do with floating point representation.
I use this function for these types of comparisons (works with values as well as with vectors):
check.equal <- function(x, y) {
check.vector <- as.logical(unlist(lapply(x, all.equal, y)))
check.vector[is.na<http://is.na/>(check.vector)] <- FALSE
return(check.vector)
}
See:
?all.equal
Hth,
Adrian
On Tue, Apr 5, 2016 at 2:34 PM, Rainer Johannes <Johannes.Rainer at eurac.edu<mailto:Johannes.Rainer at eurac.edu>> wrote:
Dear All,
I have the following problem:
I have a function in which I check if the difference between values is smaller or equal to a certain threshold. I however realized that I might get there some unexpected results:
> abs(1 - 0.95) >= 0.05
[1] TRUE
## So that’s fine, but:
> abs(1 - 0.95) <= 0.05
[1] FALSE
Apparently, abs(1 - 0.95) is not equal to 0.05, which I find however quite disturbing.
Along these lines:
> abs(0.95 - 1) > 0.05
[1] TRUE
> abs(0.95 - 1) < 0.05
[1] FALSE
I guess that has to do with the floating point representation of the data?
Is there something I miss or is there any solution to this?
Thanks for any help!
cheers, jo
I tried this on different R-version (including 3.2.3 and 3.3.0 alpha); The R-version I used for the code above is:
> sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-unknown-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
______________________________________________
R-help at r-project.org<mailto:R-help at r-project.org> mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
and provide commented, minimal, self-contained, reproducible code.
--
Adrian Dusa
University of Bucharest
Romanian Social Data Archive
Soseaua Panduri nr.90
050663 Bucharest sector 5
Romania
[[alternative HTML version deleted]]
More information about the R-help
mailing list