[R] The "less than" (<) operator doesnt seem to perform as expected

R. Michael Weylandt michael.weylandt at gmail.com
Thu Feb 2 17:03:21 CET 2012


It's likely an infelicity of floating point representations (R FAQ
7.31) but admittedly, not a case I would have expected to present
itself.

If you want it to work out as expected, try this:

l <- 0.6
u <- seq(0.4, 0.7, 0.1)

l.int <- (6L) / 10
u.int <- seq(4, 7) / 10

l < u
l.int < u.int

Michael

On Thu, Feb 2, 2012 at 5:00 AM, Jonas Hal <JOH at brf.dk> wrote:
> The example here puzzles me. It seems like the < operator doesn't work as expected.
>
>> l <- 0.6
>> u <- seq(0.4, 0.7, 0.1)
>> u
> [1] 0.4 0.5 0.6 0.7
>> mygrid <- expand.grid("l" = l, "u" = u)
>> mygrid
>    l   u
> 1 0.6 0.4
> 2 0.6 0.5
> 3 0.6 0.6
> 4 0.6 0.7
>> mygridcollapsed <- mygrid[mygrid$l < mygrid$u, ]
>> mygridcollapsed
>    l   u
> 3 0.6 0.6
> 4 0.6 0.7
>
> In this little example I expect 'mygridcollapsed' only to return row 4 and for it to return row 3 seems wrong. The strange thing is it seems to work if I start the u-sequence at 0.5.
>
>> l <- 0.6
>> u <- seq(0.5, 0.7, 0.1)
>> u
> [1] 0.5 0.6 0.7
>> mygrid <- expand.grid("l" = l, "u" = u)
>> mygrid
>    l   u
> 1 0.6 0.5
> 2 0.6 0.6
> 3 0.6 0.7
>> mygridcollapsed <- mygrid[mygrid$l < mygrid$u, ]
>> mygridcollapsed
>    l   u
> 3 0.6 0.7
>
> Maybe I'm missing something...
>
> Best wishes
> Jonas Hal
>
>
> _________________________________________________________________________________________________________________________________________
> BRFkredit sender e-mails og vedhaeftede dokumenter i ikke-krypteret form. Hvis du ikke ?nsker at modtage oplysninger fra BRFkredit pr. e-mail, beder vi dig meddele os det via brev eller e-mail. Denne e-mail kan indeholde fortrolig information. Hvis du modtager e-mailen ved en fejl, beder vi dig informere os om det hurtigst muligt. Samtidig beder vi dig slette e-mailen uden at videresende eller kopiere indholdet.
> _________________________________________________________________________________________________________________________________________
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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