我有一个in对象中每个顶点的邮政编码。我希望使用ggmap将这些转换成地理坐标,以便计算edge属性=地理距离。
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" )我想我可以这样为每个顶点生成地理坐标:
V(g)$coordinate <- geocode(V(g)$postcode, sensor = FALSE,
output = "latlon", source = "google")结果是每个顶点重复的所有顶点的latlon坐标列表,而不是每个顶点的唯一latlon。
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数=经度。我做错了什么?
发布于 2017-06-22 01:46:26
问题是,geocode返回一个数据,但是当您将它分配给V(g)$coordinate时,它将它作为一个列表来处理,并且回收这些列以获得每个顶点的值。
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的每一行转换为可以分配给顶点的元素。这可以通过很多方法来完成,这里有一个简单的方法:
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.74895https://stackoverflow.com/questions/44669213
复制相似问题