# [R] indexing operation based upon a sequence

John Smith johsmi9933 at inbox.com
Thu Feb 7 18:47:51 CET 2013

```I think this is because some decimal numbers with a fixed
number of digits after the decimal point cannot be represented
as base-2 floating point numbers with a limited number of
digits. So when one adds them up, the result is slightly
different from the sum:

huh1 = seq(.1,.7,.15)
huh1 - 0.55
[1] -4.500000e-01 -3.000000e-01 -1.500000e-01 -1.110223e-16  1.500000e-01

for "==" to give TRUE, the numbers have to be exactly identical though.

R shows those numbers rounded to two places, so the difference is
not immediately visible.

> -----Original Message-----
> From: j.braeken at uvt.nl
> Sent: Thu, 7 Feb 2013 16:41:49 +0000
> To: r-help at r-project.org
> Subject: [R] indexing operation based upon a sequence
>
> Dear R-list,
>
>
> We stumbled upon some weird problem when performing a simple indexing
> operation.
> Below some example code to illustrate the issue
>
> #FAILS TO FIND .55 Oo
> huh1 = seq(.1,.7,.15);huh1
> # [1] 0.10 0.25 0.40 0.55 0.70
> huh1 == .25
> # [1] FALSE  TRUE FALSE FALSE FALSE
> huh1 == .55
> # [1] FALSE FALSE FALSE FALSE FALSE
> huh1 == "0.55"
> # [1] FALSE FALSE FALSE  TRUE FALSE
>
> Hence somehow one element in the sequence has become a character?
>
> #DOES NOT FAIL TO FIND .55
> huh2 = c(.1,.25,.4,.55,.7);huh3
> # [1] 0.10 0.25 0.40 0.55 0.70
> huh2 == .25
> # FALSE  TRUE FALSE FALSE FALSE
> huh2 == .55
> # [1] FALSE FALSE FALSE  TRUE FALSE
>
> This is what you would expect.
>
> #DOES NOT FAIL TO FIND .55
> huh3 = seq(.1,.7,.05);huh2
> # [1] 0.10 0.25 0.40 0.55 0.70
> huh3 == .25
> # [1] FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
> FALSE FALSE
> huh3 == .55
> # [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE
> FALSE FALSE
>
> Strangely when making a longer sequence, the weird behavior does not
> persist.
>
>
> Does anyone have a clue what is going on here?
>
>
>                _
> platform       i386-pc-mingw32
> version.string R version 2.12.1 (2010-12-16)
>
>
> Best regards,
>
>
> Johan
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help