在边列表R中匹配顶点属性

时间:2019-04-22 23:12:31

标签: r igraph

我有一个定向网络列表

list(structure(list(nominator1 = structure(c(3L, 4L, 1L, 2L), .Label = c("Angela", 
"Jeff", "Jim", "Pam"), class = "factor"), nominee1 = structure(c(1L, 
2L, 3L, 2L), .Label = c("Andy", "Angela", "Jeff"), class = "factor")), class = "data.frame", row.names = c(NA, 
-4L)), structure(list(nominator2 = structure(c(4L, 1L, 2L, 3L
), .Label = c("Eric", "Jamie", "Oscar", "Tim"), class = "factor"), 
    nominee2 = structure(c(1L, 3L, 2L, 3L), .Label = c("Eric", 
    "Oscar", "Tim"), class = "factor")), class = "data.frame", row.names = c(NA, 
-4L)))

我有一个不同网络中人们的顶点属性的数据框

structure(list(names = structure(c(6L, 7L, 5L, 2L, 1L, 8L, 3L, 
4L), .Label = c("Andy", "Angela", "Eric", "Jamie", "Jeff", "Jim", 
"Pam", "Tim"), class = "factor"), gender = structure(c(3L, 2L, 
3L, 2L, 3L, 1L, 1L, 2L), .Label = c("", "F", "M"), class = "factor"), 
    happiness = c(8, 9, 4.5, 5.7, 5, 6, 7, 8)), class = "data.frame", row.names = c(NA, 
-8L))

我想找到一种方法来匹配并向网络中每个人的图形对象添加正确的顶点属性,以便我可以基于这些顶点属性执行分析。

我该如何在igraph图形对象内的边列表中匹配顶点属性?

要将边缘列表转换为图形对象,请使用

if(!require(igraph)) install.packages("igraph"); require(igraph)
graphs_list<-lapply(name_of_edgelist_list, graph_from_data_frame)

1 个答案:

答案 0 :(得分:1)

这不是一个完美的答案,但这仅适用于for循环方式

for(i in 1:length(graph_list)){
  graph_list[[i]]=set_vertex_attr(graph_list[[i]],"gender", value=attribute_df$gender[match(V(graph_list[[i]])$name, attribute_df$names)])

}

其中graph_list代表图形对象的列表,attribute_df是您拥有的属性的数据框,即

structure(list(names = structure(c(6L, 7L, 5L, 2L, 1L, 8L, 3L, 
4L), .Label = c("Andy", "Angela", "Eric", "Jamie", "Jeff", "Jim", 
"Pam", "Tim"), class = "factor"), gender = structure(c(3L, 2L, 
3L, 2L, 3L, 1L, 1L, 2L), .Label = c("", "F", "M"), class = "factor"), 
    happiness = c(8, 9, 4.5, 5.7, 5, 6, 7, 8)), class = "data.frame", row.names = c(NA, 
-8L))

然后,您可以更改循环(例如执行attribute_df$happiness以获取幸福属性)以获取每个属性。