如何恢复工会后的属性? [英] How to restore attribute after union n igraphs?

查看:149
本文介绍了如何恢复工会后的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有 n igraphs对象 g1 g2 ,.., gn 。他们是无向和加权图,即新权重的属性应该被添加。我想将 n 图合并到加权图 g 中。
$ b 如果 n从文档中已知(参见?graph.union 图具有权重属性,通过添加 _1 (和 _3 等)后缀,即 weight_1 weight_2 ,..., weight_n

我已经看过回答并为 n = 3 图表编写代码(见下文)。



编辑:

  library )

rm(list = ls(all = TRUE))#删除所有对象

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

E(g1)$ weight <-c(1,2) (5,6)


(b)
E(g2)$重量c < - union(g1,g2,g3)

new_attr < - as.list(list.edge.attributes(g))
k < - length(new_attr)#number新属性

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)})


解决方案

我已经找到解决方案 for循环

 #删除带有后缀
的边缘属性(i in 1:k){g < - delete_edge_attr(g,new_attr [i])}


let's say I have n igraphs objects g1, g2,.., gn. They are undirected and weighted graphs, i.e. new weight's attribute should be added. I'd like to union n graphs into the weighted graph g.

It is known from the documentation (see ?graph.union) if the n graphs have the weight attribute, it is renamed by adding a _1 and _2 (and _3, etc.) suffix, i.e. weight_1, weight_2,..., weight_n.

I have seen the answer and wrote the code for n=3 graphs (see below).

Edited:

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

Question. How to rewrite the last tree commands with the lapply() function?

My attempt does not work:

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

解决方案

I have found the solution with for-loop

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

这篇关于如何恢复工会后的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆