[R] I can't get seq to behave how I think it should

Martin Maechler m@ech|er @end|ng |rom @t@t@m@th@ethz@ch
Fri Jan 18 16:47:35 CET 2019


>>>>> PIKAL Petr 
>>>>>     on Thu, 17 Jan 2019 13:52:39 +0000 writes:

    > Hi
    > Or you could use rounding.

yes.

    > which(round(lut, 3)==1.8)
    > [1] 401

no!   This may work accidentally here, but in principle still
suffers for the same reasons as
the infamous FAQ 7.31 "Why doesn’t R think these numbers are equal?"
(link below) gives.

To be sure you should round to *integer*s (or other multiples of
2 ^{-k}, k \in {0,1,...,31}).

1.8 is not exactly representable as a (double precision)
floating point number in binary representation.
Your example here works because the rounding typically happens
to end up with the same binary repr ... this all relies on too
many details to be recommendable.

Martin Maechler
ETH Zurich and R Core team


    >> -----Original Message-----
    >> From: R-help <r-help-bounces using r-project.org> On Behalf Of Ben Tupper
    >> Sent: Thursday, January 17, 2019 2:43 PM
    >> To: POLWART, Calum (COUNTY DURHAM AND DARLINGTON NHS
    >> FOUNDATION TRUST) <calum.polwart using nhs.net>
    >> Cc: r-help using r-project.org
    >> Subject: Re: [R] I can't get seq to behave how I think it should
    >> 
    >> Hi,
    >> 
    >> This looks like a floating point reality bump - see
    >> 
    >> https://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-
    >> these-numbers-are-equal_003f <https://cran.r-project.org/doc/FAQ/R-
    >> FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f>
    >> 
    >> You can use other methods to finding your row - I would opt for findInterval()
    >> 
    >> > lut = seq(1.4, 2.1, by=0.001)
    >> > findInterval(1.8, lut)
    >> [1] 401
    >> 
    >> findInterval() uses a rapid search to find the index in the look up table (lut) that
    >> is just less than  or equal to the search value (in your example 1.8).
    >> 
    >> Cheers,
    >> Ben
    >> 
    >> > On Jan 17, 2019, at 8:33 AM, POLWART, Calum (COUNTY DURHAM AND
    >> DARLINGTON NHS FOUNDATION TRUST) via R-help <r-help using r-project.org>
    >> wrote:
    >> >
    >> > I am using seq with the expression seq(1.4, 2.1, by=0.001) to create a
    >> > sequence of references from 1.4 to 2.1 in 0.001 increments.  They
    >> > appear to be created correctly.  They have a related pair of data
    >> > which for the purposes of this we will call val.  I'm interested in
    >> > the content on the row with seq = 1.8. But I can't seem to get it
    >> > returned.  I can get other values but not 1.8!  yet looking at row 401
    >> > there is nothing to indicate an issue
    >> >
    >> >> a = 1.4
    >> >> b = 2.1
    >> >> seq = seq(a, b, by=0.001)
    >> >> val = ceiling(seq * 50)
    >> >> s=data.frame(seq, val)
    >> >> s$val[seq==1.799]
    >> > [1] 90
    >> >> s$val[s$seq==1.8]
    >> > numeric(0)
    >> >> s$val[seq==1.8]
    >> > numeric(0)
    >> >> s$val[s$seq==1.800]
    >> > numeric(0)
    >> >> s$val[s$seq==1.801]
    >> > [1] 91
    >> >> head(s[s$seq>1.798,])
    >> >      seq val
    >> > 400 1.799  90
    >> > 401 1.800  90
    >> > 402 1.801  91
    >> > 403 1.802  91
    >> > 404 1.803  91
    >> > 405 1.804  91
    >> >
    >> >
    >> > Can anyone explain what's going on here and how I would correctly find the
    >> content of row 401 by using an expression to equal the seq column?
    >> >
    >> >
    >> >
    >> >
    >> >
    >> >
    >> *******************************************************************
    >> ***
    >> > **********************************************
    >> >
    >> > This message may contain confidential information. If
    >> > ...{{dropped:25}}
    >> 
    >> ______________________________________________
    >> R-help using 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.
    > Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních partnerů PRECHEZA a.s. jsou zveřejněny na: https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about processing and protection of business partner’s personal data are available on website: https://www.precheza.cz/en/personal-data-protection-principles/
    > Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a podléhají tomuto právně závaznému prohláąení o vyloučení odpovědnosti: https://www.precheza.cz/01-dovetek/ | This email and any documents attached to it may be confidential and are subject to the legally binding disclaimer: https://www.precheza.cz/en/01-disclaimer/

    > ______________________________________________
    > R-help using 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.



More information about the R-help mailing list