[英]R igraph: can't set the attributes of an edge sequence
我正在嘗試根據某些標准使用 edge_attr(或者,set_edge_attr)來操作 R 中 igraph object 中邊緣子集的屬性。 例如,在下面的代碼中,我試圖將權重 = 1 的邊的年齡屬性加倍。
nodes <- data.frame(name=c('1', '4', '5', '6', '8'))
edges <- data.frame(
from = c('1', '4', '5', '1', '8', '1'),
to = c('4', '5', '6', '8', '6', '6'),
weight = c(1, 1, 1.5, 1.5, 2.5, 5),
age=c(48, 33, 45, 34, 21, 56)
)
graph = graph_from_data_frame(d = edges, vertices = nodes, directed=FALSE)
edgeseq = E(graph)[[weight==1]]
newage <- edge_attr(graph, "age", index = edgeseq)*2
edge_attr(graph, "age", edgeseq) <- newage
#Alternatively:
set_edge_attr(graph, "age", edgeseq, newage)
但是,這會引發錯誤:
Error in `[[<-`(`*tmp*`, index, value = value) :
attempt to select more than one element in vectorIndex
當我設置沒有邊緣序列的屬性時,不會發生錯誤。 任何幫助將非常感激!
select 邊緣的正確方法是:
edgeseq = E(graph)[weight==1]
注意區別:
E(graph)[weight==1]
+ 2/6 edges from 33d7121 (vertex names):
[1] 1--4 4--5
E(graph)[[weight==1]]
+ 2/6 edges from 33d7121 (vertex names):
tail head tid hid weight age
1 1 4 1 2 1 96
2 4 5 2 3 1 66
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.