我有一些大型的igraph
对象代表社交网络。所有节点都有各种属性,其中sector
是一个因子变量。我已将这个大型网络缩小为一个小的区域,其中顶点表示组,并且边具有原始网络中各个边的总和。第二个网络中的label
属性代表第一个网络中的sector
属性。
groupnet <- contract(g, as.integer(as.factor(V(g)$sector)), "ignore")
E(groupnet)$weight <- 1
groupnet <- simplify(groupnet, edge.attr.comb = list(weight = "sum"))
V(groupnet)$label <- levels(as.factor(V(g)$sector))
我想向第二个对象V(groupnet)$groupsize
添加另一个属性,该属性表示收缩到groupnet
中的原始顶点的数量。我已经用以下代码尝试过了,但是没有用:
V(groupnet)$groupsize <- length(V(g)$sector[V(g)$sector == V(groupnet)$label])
如何正确执行此操作?
答案 0 :(得分:0)
table()
在这里可能会有所帮助。试试:
set.seed(1234)
library(igraph)
g <- make_ring(1000)
V(g)$sector <- factor(sample(LETTERS, 100, replace = T))
V(g)$sector
## contracted network
groupnet <- contract(g, as.integer(as.factor(V(g)$sector)), "ignore")
E(groupnet)$weight <- 1
V(groupnet)$label <- levels(as.factor(V(g)$sector))
## number of original vertices that were contracted into groupnet
# the tip is to see that table(V(g)$sector) provides the number of vertices per sector and
# its output is also arranged like V(groupnet)
table(V(g)$sector)
V(groupnet)
# solution
V(groupnet)$groupsize <- as.numeric(table(V(g)$sector))