[R] Problem with <= (less than or equal): not giving the expected result
Thierry Onkelinx
thierry.onkelinx at inbo.be
Tue Apr 5 16:07:32 CEST 2016
You could use something like this
x <- abs(0.95 - 1)
treshold <- 0.05
x < treshold | abs(x - treshold) < 1e-6
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and
Forest
team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium
To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to say
what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey
2016-04-05 14:46 GMT+02:00 Rainer Johannes <Johannes.Rainer op eurac.edu>:
> 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 op unibuc.ro<mailto:
> dusa.adrian op 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 op eurac.edu
> <mailto:Johannes.Rainer op 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 op r-project.org<mailto:R-help op 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]]
>
> ______________________________________________
> R-help op 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
> and provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]
More information about the R-help
mailing list