2
n
igraphs 개체 g1
, g2
, .., gn
이 있다고 가정 해 봅니다. 그들은 방향이없고 가중치가있는 그래프입니다. 즉, 새 가중치의 속성이 추가되어야합니다. n
그래프를 가중 그래프 g
에 결합하고 싶습니다.통합 후 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)})