0

我有以下数据集:

longitude   latitude
NA  NA
NA  NA
NA  NA
NA  NA
-73.77384042    40.64582098
NA  NA
NA  NA
NA  NA
NA  NA
NA  NA

我正在使用下面的代码来转换纬度和经度以使用以下代码获取位置:

tweets1.df$textAddress <- mapply(FUN = function(lon, lat) revgeocode(c(lon, lat)), tweets1.df$longitude, tweets1.df$latitude)

但我收到以下错误: Error: is.numeric(location) && length(location) == 2 is not TRUE

请让我知道我哪里出错了。

4

1 回答 1

0

这应该有效:

options(digits=16)
indices <- which(complete.cases(tweets1.df[,1:2]))
tweets1.df$textAddress <- NA
tweets1.df[indices,]$textAddress <- mapply(FUN = function(lon, lat) 
                    revgeocode(c(lon, lat)), 
                    tweets1.df[indices,]$longitude, 
                    tweets1.df[indices,]$latitude)

tweets1.df


longitude    latitude                        textAddress
1            NA          NA                               <NA>
2            NA          NA                               <NA>
3            NA          NA                               <NA>
4            NA          NA                               <NA>
5  -73.77384042 40.64582098 Terminal 5, Jamaica, NY 11430, USA
6            NA          NA                               <NA>
7            NA          NA                               <NA>
8            NA          NA                               <NA>
9            NA          NA                               <NA>
10           NA          NA                               <NA>

你也可以试试这个,稍微修改一下评论中的建议:

tweets1.df$textAddress <- apply(tweets1.df, 1, function(x){print(x);ifelse(any(is.na(x)), NA, revgeocode(x))})
于 2016-10-14T06:00:29.537 回答