[R] problem of subscript value from vector and list

Jingru Dai jingru.dai at sci.monash.edu.au
Thu Feb 28 04:44:52 CET 2008


Hi, everyone

I got some problems when trying to subscript the value of vector and 
list, by using calculated indices.
Here is the vector I am generated
lon<-rep(0,886);lat<-rep(0,691)
for (i in 1:886){
    lon[i]<-112+0.05*(i-1)
    }
for (i in 691:1){
    lat[i]<--44.5+0.05*(691-i)
    }
For a given location of lon(xp) and lat(yp), I would like to calculate 
the  position of them, and using that to get the value for the location 
from one list.
xp<-c(112,112.05); yp<-c(-10,-10.10)

x<-rep(0,length(xp)); y<-rep(0,length(yp))
for (i in 1:length(xp)){
    x[i]<-(xp[i]-112)/0.05+1
    }
for(j in 1:length(yp)){
    y[j]<-(-10-yp[j])/0.05+1
    }
So here the value of x and y should indicate the position where xp and 
yp are in the vector lon and lat.  And it appears to be the right 
number.  But when I tried to retrieve the value of lon using the 
position, it returns the wrong value.  Here's the result I got
 > x
[1] 1 2
 > lon[x]
[1] 112 112
 > lon[c(1,2)]
[1] 112.00 112.05
 > y
[1] 1 3
 > lat[y]
[1] -10.00 -10.05
 > lat[c(1,3)]
[1] -10.0 -10.1

I have no idea why it returns the wrong value when I subscripts by x, 
while it works perfectly fine when I subscripts with the value of x 
directly.  Is there any special rule of subscripts?  My version of R is 
2.5.1.
Thanks a lot

Jingru Dai
 ----
 School of Mathematical Sciences
 Rm 454, Building 28
 Monash University, 3800
 Victoria, Australia



More information about the R-help mailing list