在 R 中使用 IGraph,如何从连接到每个顶点的边计算 highest/lowest 边属性值?

Using IGraph in R, how to calculate highest/lowest edge attribute value from those connected to each vertex?

我有一个图 g,其中包含一堆 nodes/vertexes,它们由边连接,每条边都有特定的数字属性值。边缘列表可能如下所示:

v1 v2 att
a b 17
a c 6
a d 2
b c 9

出于可视化目的,我需要添加连接到每个顶点的最高和最低 att 数字(来或去;方向在这里无关紧要)。

例如,我想将 a 的 v$max 设置为 17,将 a 的 v$min 设置为 2,因为 link 的最大 att-value 连接到 a是17,最小的是2。

我根据这个主题尝试了一些变体:

V(g)$min=min(E(g)[incident(g,V(g), mode='total')]$att)

这似乎非常接近正确,因为当我在单个节点上尝试时它似乎给出了正确的答案,如

min(E(g)[incident(g,V(g)[2], mode='total')]$att)

但是当我在所有节点上尝试它时它不起作用。目的是为每个顶点设置 V(g)$minV(g)$max

希望这不是作业问题,您只是漏掉了泛化部分

library(igraph)
library(plyr)
df <- data.frame(read.table(text="v1 v2 weight
a b 17
a c 6
a d 2
b c 9", header=T))
df$weight <- as.numeric(df$weight)

#transform df into igraph
g <- graph.data.frame(df, directed=F, vertices=NULL)    

#for each vertex, get all incidental edges, weights and use range to find min & max
ldply(V(g), function(v) range(incident(g, v, mode='total')$weight))