[R] vector is not assigned correctly in for loop

Berend Hasselman bhh at xs4all.nl
Sat Oct 6 12:25:06 CEST 2012


On 06-10-2012, at 08:14, 周果 <guo.chow at gmail.com> wrote:

> Hi there,
> Here is a minimum working example:
> ----------------------------------------------------------------
> lower = 0
> upper = 1
> n_bins = 50
> interval = (upper - lower) / n_bins
> bins = vector(mode="numeric", length=n_bins)
> breaks = seq(from=lower  + interval, to=upper, by=interval)
> 
> for(idx in breaks)
> {
> bins[idx / interval] = idx
> }
> 
> print(bins)
> ----------------------------------------------------------------
> which outputs:
> ----------------------------------------------------------------
> [1] 0.02 0.04 0.06 0.08 0.10 0.14 0.00 0.16 0.20 0.00 0.22 0.24 0.26 0.28
> [15] 0.30 0.32 0.34 0.36 0.38 0.40 0.42 0.44 0.46 0.48 0.50 0.52 0.54 0.56
> [29] 0.58 0.60 0.62 0.64 0.66 0.68 0.70 0.72 0.74 0.76 0.78 0.80 0.82 0.84
> [43] 0.86 0.88 0.90 0.92 0.94 0.96 0.98 1.00
> ----------------------------------------------------------------
> It turns out that some elements are incorrect, such as the 6th
> element 0.14, which should be 0.12 in fact.

And the 7th is also incorrect.

> Is this a bug or I am missing something?

It is not a bug in R.
Yes you are indeed missing something. Read R FAQ 7.31.
Answer is: floating point inaccuracy.

Insert

print(formatC(idx/interval,format="f",digits=17))   
print(as.integer(idx/interval))

immediately after the opening { of the for loop.
If you insist on copying breaks to bins in the way you are doing you could use round(idx/interval,3) for example.

Berend




More information about the R-help mailing list