在r中循环中的分类变量值索引
我正在尝试通过R和OSMDATA软件包获得各个城市的坐标。 我的代码看起来像这样,
cities<-c("Name1","Name2")
for (city in cities){
city <-getbb(
city,
format_out = "matrix",
base_url = "https://nominatim.openstreetmap.org",
featuretype = "settlement",
limit = 10,
)
}
我希望坐标归因于循环中的循环中的城市的实际名称,例如,循环的第一个迭代
Name1<- getbb("Name1")
等等,但我找不到一种方法来索引getBB()的输出在循环内部具有getBB()函数中变量名称的函数。
我不想使用数字来索引坐标,因为我有很多城市。
我该怎么做?
I am trying to get coordinates of various cities with R and the osmdata package.
My code looks like this
cities<-c("Name1","Name2")
for (city in cities){
city <-getbb(
city,
format_out = "matrix",
base_url = "https://nominatim.openstreetmap.org",
featuretype = "settlement",
limit = 10,
)
}
I would like the coordinates to be attributed to the actual name of the city in the loop e.g. for the first iteracy of the loop
Name1<- getbb("Name1")
and so on, but I cannot find a way to index the output of the getbb() function inside the loop with the name of the variable inside the getbb() function.
I do not want to use a number to index the coordinates as I have many cities.
How can I do it ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(1)
编写循环时,您需要考虑如何存储每次迭代的输出。我怀疑
getBb
的输出非常复杂(也许是两个列矩阵?“ bb”用于“边界框”?),所以我将使用“列表”进行存储。最后,您可以使用城市名称从列表中提取值:
When you write a loop, you need to think about how to store the output of each iteration. I suspect that the output of
getbb
is quite complicated (maybe a two column matrix? "bb" for "bounding box"?), so I would use a "list" for storage.In the end, you can use city name to extract value from the list: