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

Sarah Goslee sarah.goslee at gmail.com
Thu Feb 2 16:54:25 CET 2012


This is R FAQ 7.31, about machine representation of
floating point numbers.

> mygrid$u[3] - mygrid$l[3]
[1] 1.110223e-16

So
mygrid$l[3] < mygrid$u[3]
is true, though the difference is very, very small and due solely
to the limitations of computers.

Sarah

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
>
>

-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list