首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用ggmap对iGraph顶点属性进行地理编码

用ggmap对iGraph顶点属性进行地理编码
EN

Stack Overflow用户
提问于 2017-06-21 07:15:51
回答 1查看 126关注 0票数 1

我有一个in对象中每个顶点的邮政编码。我希望使用ggmap将这些转换成地理坐标,以便计算edge属性=地理距离。

代码语言:javascript
复制
require(igraph)
require(ggmap)

g <- graph.ring(6)
V(grph)$postcode <- c("Johannesburg 2017", 
                      "Rondebosch 8000",
                      "Durban 4001", 
                      "Pietermaritzburg 3201", 
                      "Jeffreys Bay 6330", 
                      "Pretoria 0001" )

我想我可以这样为每个顶点生成地理坐标:

代码语言:javascript
复制
V(g)$coordinate <- geocode(V(g)$postcode, sensor = FALSE, 
                           output = "latlon", source = "google")

结果是每个顶点重复的所有顶点的latlon坐标列表,而不是每个顶点的唯一latlon。

代码语言:javascript
复制
head(head(V(g)$coordinate)
[[1]]
[1] 28.03837 28.31993 31.02204 30.36661 24.91015 28.18540

[[2]]
[1] -26.18825 -25.84222 -29.84962 -29.65119 -34.05067 -25.74895

[[3]]
[1] 28.03837 28.31993 31.02204 30.36661 24.91015 28.18540

[[4]]
[1] -26.18825 -25.84222 -29.84962 -29.65119 -34.05067 -25.74895

[[5]]
[1] 28.03837 28.31993 31.02204 30.36661 24.91015 28.18540

[[6]]
[1] -26.18825 -25.84222 -29.84962 -29.65119 -34.05067 -25.74895

-ve数=纬度+ve数=经度。我做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-22 01:46:26

问题是,geocode返回一个数据,但是当您将它分配给V(g)$coordinate时,它将它作为一个列表来处理,并且回收这些列以获得每个顶点的值。

代码语言:javascript
复制
postcode_df <- geocode(V(g)$postcode, sensor = FALSE, 
                           output = "latlon", source = "google")

postcode_df
#        lon       lat
# 1 28.03837 -26.18825
# 2 28.31993 -25.84222
# 3 31.02204 -29.84962
# 4 30.36661 -29.65119
# 5 24.91015 -34.05067
# 6 28.18540 -25.74895

您需要将dataframe的每一行转换为可以分配给顶点的元素。这可以通过很多方法来完成,这里有一个简单的方法:

代码语言:javascript
复制
V(g)$coordinate <- split(postcode_df, 1:nrow(postcode_df))

V(g)$coordinate
# [[1]]
# lon       lat
# 1 28.03837 -26.18825
# 
# [[2]]
# lon       lat
# 2 28.31993 -25.84222
# 
# [[3]]
# lon       lat
# 3 31.02204 -29.84962
# 
# [[4]]
# lon       lat
# 4 30.36661 -29.65119
# 
# [[5]]
# lon       lat
# 5 24.91015 -34.05067
# 
# [[6]]
# lon       lat
# 6 28.1854 -25.74895
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44669213

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档