2
私はn
igraphsオブジェクトg1
,g2
、..、gn
を持っているとします。それらは無向グラフであり、重み付けされたグラフであり、すなわち新しい重みの属性が追加されるべきである。 n
グラフを加重グラフg
に結合したいと思います。ユニオンn igraphsの後に属性を復元する方法は?
(?graph.union
を参照)n
グラフはweight
属性を持っている場合、それは(などと_3
、)_1
と_2
を追加することによって、名前が変更された接尾辞、すなわちweight_1
、weight_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)})