【发布时间】:2022-01-08 07:54:36
【问题描述】:
我正在尝试通过列表循环 tidygeocoder 包中的 reverse_geo() 函数。
当我将函数应用于单个数据框时,它看起来像这样:
library(tidyverse, warn.conflicts = FALSE)
library(tidygeocoder)
num_coords <- 25 # number of coordinates
set.seed(103) # for reproducibility
# latitude and longitude bounds
lat_limits <- c(40.40857, 40.42585)
long_limits <- c(-3.72472, -3.66983)
# randomly sample latitudes and longitude values
random_lats <- runif(
num_coords,
min = lat_limits[1],
max = lat_limits[2]
)
random_longs <- runif(
num_coords,
min = long_limits[1],
max = long_limits[2]
)
# Reverse geocode the coordinates
# the speed of the query is limited to 1 coordinate per second to comply
# with Nominatim's usage policies
madrid <- reverse_geo(
lat = random_lats, random_longs,
method = 'osm', full_results = TRUE,
custom_query = list(extratags = 1, addressdetails = 1, namedetails = 1)
)
这有效并返回结果。
现在,当我尝试使用 lapply 将其应用于列表的每个元素时:
#NOW try to use reverse_geo() looping through a list
df1<- data.frame(random_lats,random_longs)
df2<- data.frame(random_lats,random_longs)
list <- list(df1, df2)
data_frame_list = lapply(list, function(x) reverse_geo (lat = x[["random_lats"]], long = x[["random_longs"]], "osm",
TRUE, list(extratags = 1, addressdetails = 1, namedetails = 1)))
我收到错误:错误:limit must be NULL or >= 1. See ?reverse_geo
我认为这是因为 reverse_geo() 没有正确看到 lat 和 long 向量,因为我没有正确传递给 lapply()。
关于如何使用 lappyly 或 purrr::map() 解决此问题的任何建议?我不喜欢使用其中一种——只是想避免使用 for 循环。
【问题讨论】:
-
您需要提供数据以使其可重现。但是,我不清楚你在这里做什么。为什么
reverse_geo(lat, long, method, full_results, custom_query)作为第一个参数传递给lapply?第一个参数应该是列表。然后您可以假设其中的每个数据框都可以作为x访问。lapply(data_frame_list, function(x) reverse_geo(lat = x[["lat"]], ... -
@caldwellst 嗨,我在编辑后的帖子中添加了一个可重现的示例。如果有任何想法,将不胜感激!