[R] problem of subscript value from vector and list

From: Jingru Dai <jingru.dai_at_sci.monash.edu.au>
Date: Thu, 28 Feb 2008 14:44:52 +1100


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

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu 28 Feb 2008 - 03:48:27 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 28 Feb 2008 - 16:30:17 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive