# [R] Problem (bug?) with vector indices

Rodrigo Drummond rduarte at unicamp.br
Tue Aug 31 22:08:00 CEST 2004

```Hi all,

I found a problem on R that looks like a bug to me. I am working with R
for windows verson 1.9.0, but the problem also happens on the linux
version 1.9.0. I hope that the problem is on my reasoning, and I would be
grateful if someone could explain me why this happens.
There goes the example:

> x<-seq(-2,2,0.001)
> y<-rep(0,length(x))
> for (i in x){y[1000*i+2001]<-i^2}
> plot(x,y)    # Here I found something wrong
> which(y==0)
[1]  244  353  357  361  365  369  482  486  490  494  579  583  587  591
595
[16]  599  603  607  611  615  619  708  712  716  720  724  728  732  736
740
[31]  744  837  841  845  849  853  857  861  865  869  962  966  970  974
978
[46]  982  986  990  994  998 2001
> x[244]
[1] -1.757
> i<-x[244]
> 1000*i+2001
[1] 244
> y[1000*i+2001]
[1] 3.087049
> y[244]
[1] 0
> y[243]
[1] 3.087049

What means that the expression 1000*i+2001, which is equal to 244, inside
the brackets is evaluated as 243! The same occurs with every value of x
that results in a zero value of y, the right value is stored in y one
position before it should be, overwriting the previous value at this
position.
The bug is on R or on my mind?

Thanks a lot.
Rodrigo Drummond

____________________________________________
Rodrigo D. Drummond
Laboratorio Genoma Funcional
Centro de Biologia Molecular e Eng. Genetica