问题
I have a list of directed networks
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)))
and I have a dataframe of vertex attributes of the people in the different networks
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))
I want to find a way to match and add the correct vertex attributes to graph objects of everyone in the network so I can perform analyses based on these vertex attributes.
How would I go about matching the vertex attributes across the list of edgelists inside igraph
graph objects?
To convert edgelists to a graph object use
if(!require(igraph)) install.packages("igraph"); require(igraph)
graphs_list<-lapply(name_of_edgelist_list, graph_from_data_frame)
回答1:
Not a perfect answer, but this is for just one attribute the for loop way
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)])
}
with graph_list
representing the list of graph objects and attribute_df
being the dataframe of the attributes you had, which were
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))
Then you could just change the loop (like doing attribute_df$happiness
to get the happiness attribute) to get each of the attributes. A vectorized way would be better though
来源:https://stackoverflow.com/questions/55802228/matching-vertex-attributes-across-a-list-of-edgelists-r