通过它们连接的节点的属性计算图中的边
Counting edges in a graph by the attribute of nodes they connect
抱歉这个含糊的问题,我很难准确说明我需要什么。我希望这个例子能让它更清楚。
我有一个具有各种节点属性(如国家隶属关系等)的网络。我感兴趣的是一个国家有多少节点连接到 相同 或 不同 国家。
因此,在示例网络中:
require(igraph)
zach <- graph("Zachary") # the Zachary carate club
V(zach)$new_var <- sample(c('tomato', 'gold', 'steelblue'), gorder(zach), replace = TRUE)
plot(zach, vertex.size=10, vertex.label=NA, vertex.color = V(zach)$new_var)
我这里感兴趣的是,比如有多少个blue节点连接到blue,连接到red 等
这是一种可能的方法
ends <- as_edgelist(zach)
e1 <- V(zach)[ends[,1]]$new_var
e2 <- V(zach)[ends[,2]]$new_var
然后你可以做(用set.seed(15)
)
table(e1, e2)
# e2
# e1 gold steelblue tomato
# gold 3 9 13
# steelblue 5 4 12
# tomato 7 10 15
或者如果你想把金番茄和番茄金一样算,你可以这样做
endcolors <- t(apply(cbind(e1, e2), 1, sort))
table(endcolors[,1], endcolors[,2])
# gold steelblue tomato
# gold 3 14 20
# steelblue 0 4 22
# tomato 0 0 15
抱歉这个含糊的问题,我很难准确说明我需要什么。我希望这个例子能让它更清楚。
我有一个具有各种节点属性(如国家隶属关系等)的网络。我感兴趣的是一个国家有多少节点连接到 相同 或 不同 国家。
因此,在示例网络中:
require(igraph)
zach <- graph("Zachary") # the Zachary carate club
V(zach)$new_var <- sample(c('tomato', 'gold', 'steelblue'), gorder(zach), replace = TRUE)
plot(zach, vertex.size=10, vertex.label=NA, vertex.color = V(zach)$new_var)
我这里感兴趣的是,比如有多少个blue节点连接到blue,连接到red 等
这是一种可能的方法
ends <- as_edgelist(zach)
e1 <- V(zach)[ends[,1]]$new_var
e2 <- V(zach)[ends[,2]]$new_var
然后你可以做(用set.seed(15)
)
table(e1, e2)
# e2
# e1 gold steelblue tomato
# gold 3 9 13
# steelblue 5 4 12
# tomato 7 10 15
或者如果你想把金番茄和番茄金一样算,你可以这样做
endcolors <- t(apply(cbind(e1, e2), 1, sort))
table(endcolors[,1], endcolors[,2])
# gold steelblue tomato
# gold 3 14 20
# steelblue 0 4 22
# tomato 0 0 15