2017-03-25 10 views
2

私はn igraphsオブジェクトg1,g2、..、gnを持っているとします。それらは無向グラフであり、重み付けされたグラフであり、すなわち新しい重みの属性が追加されるべきである。 nグラフを加重グラフgに結合したいと思います。ユニオンn igraphsの後に属性を復元する方法は?

これは、ドキュメントから知られている

?graph.unionを参照)nグラフはweight属性を持っている場合、それは(などと_3、)_1_2を追加することによって、名前が変更された接尾辞、すなわちweight_1weight_2、...、 weight_n

私はanswerを見て、n=3グラフのコードを書きました(下記参照)。

編集:

library(igraph) 

rm(list=ls(all=TRUE)) # delete all objects 

g1 <- graph_from_literal(A1-B1-C1) 
g2 <- graph_from_literal(A2-B2-C2) 
g3 <- graph_from_literal(A3-B3-C3) 

E(g1)$weight <- c(1, 2) 
E(g2)$weight <- c(3, 4) 
E(g3)$weight <- c(5, 6) 


g  <- union(g1, g2, g3) 

new_attr <- as.list(list.edge.attributes(g)) 
k  <- length(new_attr) # number of new attributes 

value_new_attr <- lapply(list.edge.attributes(g), 
         function(x) get.edge.attribute(g,x)) 

df <- data.frame() 
for (i in 1:k) {df <- rbind(df, value_new_attr[[i]])} 
E(g)$weight <- colSums(df, na.rm=TRUE) 

g <- delete_edge_attr(g, "weight_1") # 1 
g <- delete_edge_attr(g, "weight_2") # 2 
g <- delete_edge_attr(g, "weight_3") # 3 

質問。lapply()機能で最後のツリーコマンドを書き換える方法はありますか?

私の試みは動作しません:

g <- lapply(value_new_attr, function(x) {g <- delete_edge_attr(g, x)}) 

答えて

2

私はfor-loop

# delete edge attributes with suffix 
for (i in 1:k) {g <- delete_edge_attr(g, new_attr[i])} 
と解決策を発見しました
関連する問題