My goal is to split up a dataframe, run igraph's graph_from_data_frame on each group and combine this back into the original dataframe in some way.
So far I've been able to get the igraph function to return a list of what I think are graph parameters, but I can't tell because I can't 'see' inside the listed rows. Here is some replicable code:
set.seed(123)
Data <- data.frame(
From = sample(c("Dan", "Sharon","Bob","Andrew"), 100, replace = TRUE),
To = sample(c("Dan", "Sharon","Bob","Andrew"), 100, replace = TRUE),
Time=sample(c(1,2,3),100, replace = TRUE),
ID.match=1:100)
Data %>% View
I'd like to pull the graph measures of centrality and combine them with the ID.match variable. I then plan to regress these measures on other variables of interest already contained within my dataset. I'm using group_by on Time to create a graph for each point in time like this:
Data %>% group_by(Time) %>% do(v=graph_from_data_frame(.))
The igraph function, graph_from_data_frame, create's an igraph object from which the measures of centrality can be obtained. The following code can do what I want without group_by. I'd like to use this with group_by:
set.seed(123)
Data <- data.frame(
From = sample(c("Dan", "Sharon","Bob","Andrew"), 100, replace = TRUE),
To = sample(c("Dan", "Sharon","Bob","Andrew"), 100, replace = TRUE),
# Time=sample(c(1,2,3),100, replace = TRUE),
ID.match=1:100)
Data %>% View
g <- graph_from_data_frame(Data)
plot(g)
The plot looks like this, which is expected:
metrics <- data.frame(
Degree=degree(g),
Closeness = closeness(g),
Betweenness = betweenness(g)
)
metrics %>% View
I would like to have a 'metrics' dataframe for each group. This question is similar to this SO question, but I can't seem to get things worked out. I've tried to use the purrr package to unlist the listed dataframe, but I think it's a bit too advanced for me. Any help would be much appreciated.