【发布时间】:2016-10-09 23:41:35
【问题描述】:
我正在尝试从 spatialLinesDataFrame 转换为 igraph 对象,并认为我可能会丢失我想要保留的信息。 igraph 相当新,所以请多多包涵。下面的例子说明:
# create sldf object
require(sp); require(igraph); require(shp2graph)
d = data.frame(x = c(0,80,100,0,-20,-8,0,3,-10,-5,80,75),
y = c(0,-10,5,0,14,33,0,-4,-10,-12,-10,5),
grp = c(1,1,1,2,2,2,3,3,3,3,4,4))
sl = SpatialLines(list(
Lines(list(Line(d[d$grp == 1,1:2]),
Line(d[d$grp == 4,1:2])), ID=1),
Lines(Line(d[d$grp == 2,1:2]), ID=2),
Lines(Line(d[d$grp == 3,1:2]), ID=3))
)
sldf = SpatialLinesDataFrame(sl, iris[1:3,])
plot(sldf)
现在转换为 igraph 并绘图:
read_sldf = readshpnw(sldf, ELComputed = T)
g = nel2igraph(read_sldf[[2]], read_sldf[[3]], weight=read_sldf[[4]])
plot(g)
第一个 spdf 行 (sldf[1,]) 的分支已丢失,我说得对吗?调用 as_edgelist(g) 返回 3 行而不是 4 行。
【问题讨论】:
-
是的,完全正确。
sl2 = SpatialLines(list(Lines(Line(d[d$grp == 1,1:2]), ID=1), Lines(Line(d[d$grp == 2,1:2]), ID=2), Lines(Line(d[d$grp == 3,1:2]), ID=3))); sldf2 = SpatialLinesDataFrame(sl2, iris[1:3,]); read_sldf2 = readshpnw(sldf2, ELComputed = T)。我从SpatialLines制作的对象没有第一个Lines的第二个Line。identical(read_sldf, read_sldf2); [1] TRUE。readshpnw()显然只读了第一个Line与Lines。如果给Detailed=T,readshpnw()不会读取第二个Line。 -
感谢@cuttlefish44 的解释。我想不可能捕获物理网络(保持完整拓扑)并在必要时复制属性?
readshpnw(sldf, Detailed = T)返回Error in readshpnw(sldf, Detailed = T) : If a detailed graph is to be built, the properties of its attributes has to be specified -
据我所知,没有
ea.prop,Detailed = T就无法工作。read_sldf_d = readshpnw(sldf, ELComputed = T, Detailed = T, ea.prop = rep(0, 5))似乎至少有关于每个第一个Line的所有坐标信息。