[R] Problem with <= (less than or equal): not giving the expected result
PIKAL Petr
petr.pikal at precheza.cz
Wed Apr 6 06:42:41 CEST 2016
Hi
Or some rounding when comparing results with threshold exactly.
> round(abs(0.95 - 1),2) > 0.05
[1] FALSE
> round(abs(0.95 - 1),2) < 0.05
[1] FALSE
>
Cheers
Petr
> -----Original Message-----
> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Rainer
> Johannes
> Sent: Tuesday, April 5, 2016 4:33 PM
> To: Thierry Onkelinx <thierry.onkelinx at inbo.be>
> Cc: r-help at r-project.org; Adrian Dușa <dusa.adrian at unibuc.ro>
> Subject: Re: [R] Problem with <= (less than or equal): not giving the expected
> result
>
> Thanks!
>
> On 05 Apr 2016, at 16:07, Thierry Onkelinx
> <thierry.onkelinx at inbo.be<mailto:thierry.onkelinx at inbo.be>> wrote:
>
> 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 at eurac.edu<mailto:Johannes.Rainer at 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 at unibuc.ro<mailto:dusa.adrian at unibuc.ro><mailto:dusa.adria
> n 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/><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><mailto:
> 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><mailto: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><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 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.
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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
> and provide commented, minimal, self-contained, reproducible code.
________________________________
Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či zpožděním přenosu e-mailu.
V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi či osobě jím zastoupené známá.
This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system.
If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email.
In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.
More information about the R-help
mailing list