iGraph中边缘地理距离的计算

iGraph中边缘地理距离的计算,r,igraph,ggmap,geosphere,R,Igraph,Ggmap,Geosphere,我有iGraph对象中每个顶点的地理坐标。现在我想计算现有边之间的距离 library(igraph) library(ggmap) library(geosphere) g <- graph.ring(6) V(grph)$postcode <- c("Johannesburg 2017", "Rondebosch 8000", "Durban 4001", "Piet

我有iGraph对象中每个顶点的地理坐标。现在我想计算现有边之间的距离

library(igraph)
library(ggmap)
library(geosphere)

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

postcode_df <- geocode(V(g)$postcode, sensor = FALSE, 
                       output = "latlon", source = "google")

V(g)$coordinate <- split(postcode_df, 1:nrow(postcode_df))

V(g)$coordinate[1]
[[1]]
       lon       lat
1 28.03837 -26.18825
计算两点之间距离的一般方法是

distHaversine(p1, p2, r=6378137).

p1由el[,1]定义,p2由el[,2]定义。el[,1:2]是指g中的顶点数。所以我需要提取对应于el[,1]和el[,2]的V(g)$坐标。如蒙指教,不胜感激

这里有一个问题,
split
返回一个数据帧,我们可以通过以下方式解决:

V(g)$coordinate <- lapply(split(postcode_df, 1:nrow(postcode_df)), unlist)

这将在嵌套列表中提供答案。我需要取消所有测试,以便E(g)$distance返回一个值列表
distHaversine(p1, p2, r=6378137).
V(g)$coordinate <- lapply(split(postcode_df, 1:nrow(postcode_df)), unlist)
library(purrr)
el <- get.edgelist(g, names=FALSE)
E(g)$distance <- unlist(map2(V(g)$coordinate[el[,1]], V(g)$coordinate[el[,2]], distHaversine))