rrgooglemaps

How do you make reference to the next row using R, without looping?


I am playing with RGooglemaps, and have been able to plot lines on a map. I loaded my lats and longs from a csv into a coords object.

I wanted to imply direction using: PlotArrowsOnStaticMap

defined as:

    PlotArrowsOnStaticMap(MyMap, lat0, lon0, lat1 = lat0, lon1 = lon0, TrueProj = TRUE, FUN = arrows, add = FALSE, verbose = 1,...)

I define lat0 as something like coords[,'lat']. How do I give the lat1?

The value is the next row in the file - but how do I describle that relatively? (coords[+1,'lat'] in pseudocode.

Is there some basic reading I should be doing?


Solution

  • An inelegant work around is to create new columns for your lat and long that are upshifted by one row compared to the initial rows. The value of the first row is wrapped to the bottom (or replaced by NA if this does not make sense).

    coords$lat.1<-coords$lat[c(2:length(coords$lat), 1)]
    coords$lon.1<-coords$lon[c(2:length(coords$lon), 1)]
    

    You now have two columns for lat (lat and lat1) and two columns for long (lon, lon1).

    with(coords, PlotArrowsOnStaticMap(lat0=lat, lon0=lon, lat1=lat1, lon1=lon1...)