Using the revgeocode function in a FOR loop. Help Wanted

My problem is below:

My input is in the format as indicated in the small example below:

USERID LONGITUDE LATITUDE 1 -8.79659 55.879554 2 -6.874743 56.87896 3 -3.874743 58.87896 4 -10.874743 80.87896 

I used the following code for reverse geocoding: latitiude and longitude

 dset <- as.data.frame(dataset[,2:3]) dset <- na.omit(dset) library (ggmap) location <- dset nrow(location) locaddr <- matrix(0,nrow(location),1) location <- as.matrix(location) for (i in 1:nrow(location)) { locaddr[i,] <- revgeocode(location[i,], output = c("address"), messaging = FALSE, sensor = FALSE, override_limit = FALSE) } 

Now, a specific longitude-latitude returns NA from the Google Maps API. But when this happens, the for loop ends for some reason. I would like to get around this and continue processing the remaining data points. One of my ideas was the following pseudo code:

 if i = nrow(location) continue else repeat revgeocode for loop here end-for end-if. 

Please indicate how this can be done or if there is a better way to do this.

Thank you in advance for your time and help.

+1
loops r
source share
1 answer

No need to use for-loop here. I recommend that you use lapply to avoid a side effect, and pre-highlight problems:

  locaddr <- lapply(seq(nrow(location)), function(i){ revgeocode(location[i,], output = c("address"), messaging = FALSE, sensor = FALSE, override_limit = FALSE) }) 
+1
source share

All Articles