在FOR循环中使用revgeocode函数.需要帮助 [英] Using revgeocode function in a FOR loop. Help required

查看:118
本文介绍了在FOR循环中使用revgeocode函数.需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题如下:

我的输入数据具有以下小示例中给出的格式:

My input data is of the format as given 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 have used the follwoing code to reverse geocode the 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)



}

现在,某些特定的经纬度从Google Maps API返回NA.但是当这种情况发生时,由于某种原因,for循环终止了.我想绕过这一点,并继续处理剩余的数据点.我的一个主意是以下伪代码:

Now certain longitude-latitude return NA from Google Maps API. But when this happens the for loop is terminated for some reason. I would like to circumvent this and continue processing for the remaining data points. One idea I had was the following pseudocode:

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

请告知如何执行此操作,或者是否有更好的方法来执行此操作.

Kindly advise how this can be done or if there is a better way to do this.

预先感谢您的时间和帮助.

Thank you in advance for your time and help.

推荐答案

无需在此处使用for-loop.我建议您使用lapply以避免副作用,并预先分配问题:

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

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

这篇关于在FOR循环中使用revgeocode函数.需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆