【问题标题】:How to restore attribute after union n igraphs?如何在联合 n igraphs 后恢复属性?
【发布时间】:2017-08-18 02:22:31
【问题描述】:

假设我有n igraphs 对象g1g2、..、gn。它们是无向的加权图,即应该添加新的权重属性。我想将 n 图形合并到加权图形 g 中。

从文档中知道(参见?graph.union)如果n 图形具有weight 属性,则通过添加_1_2(和_3 等)对其进行重命名后缀,即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)})

【问题讨论】:

    标签: r for-loop attributes igraph lapply


    【解决方案1】:

    我找到了for-loop的解决方案

    # delete edge attributes with suffix
    for (i in 1:k) {g <- delete_edge_attr(g, new_attr[i])}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-11
      • 1970-01-01
      • 1970-01-01
      • 2013-12-27
      • 2018-02-05
      • 1970-01-01
      • 1970-01-01
      • 2020-12-14
      相关资源
      最近更新 更多