Skip to content
Prev 307162 / 398506 Next

vector is not assigned correctly in for loop

Hello,

This seems to be a case for FAQ 7.31 Why doesn't R think these numbers 
are equal?

See this example:

3/5 - 1/5 - 2/5  # not zero
3/5 - (1/5 + 2/5)  # not zero, different from above

In your case, try

for(idx in breaks){
     print(idx / interval, digits = 16)  # see problem indices
     bins[idx / interval] = idx
}
b2 <- breaks

identical(bins, b2)  # FALSE

What happens is that instead of 7, the value of idx/interval is 
6.9999999 with integer part 6. So bins[6] is assigned twice, first 1.2 
then this valuew is overwritten by 1.4 and bins[7] is never written to. 
The same goes with indices 9 and 10.

Avoid this type of indexing. And if possible use the vectorized 
instruction b2 <- breaks.

Hope this helps,

Rui Barradas

Em 06-10-2012 07:14, ?? escreveu: