r - Loop the dataframe to create igraph -
i have data frame, edge list r/igraph:
node_one node_two weight group 1 2 1 175221 3 4 1 175221 5 6 1 175221 7 8 1 175221 9 10 1 576546 11 12 2 576546 13 14 2 576546 15 16 2 789535 17 18 2 789535 19 20 2 789535
i want loop through df according different value of hee_provn1, create multiple graphs , graph measures.
my code here:
# install packages library(dplyr) library(igraph) df <- data.frame(node_one=seq(1,19,2), node_two=seq(2,20,2), weight = c(rep(2,5),rep(2,5)), group=c(rep(175221,4),rep(576546,3),rep(789535,3))) final.df1 <- c() for(x in unique(df$group)){ df2 <- subset(df, subset = group == x) # create graph g <- graph_from_data_frame(df2, directed=false) d = degree(g) # number of vertex's adjacent edges c = closeness(g, weight = df2$weight) # measures how many steps required access every other vertex given vertex b = betweenness(g, weight = df2$weight) # defined number of geodesics (shortest paths) going through vertex or edge result <- data.frame(x, d,c,b) final.df1 <- rbind(final.df1, result) } colnames(final.df1) <- c('group','degree','closeness','betweeness')
the head of final result
group degree closeness betweeness 1 175221 1 0.02 0 3 175221 1 0.02 0 5 175221 1 0.02 0 7 175221 1 0.02 0 2 175221 1 0.02 0 4 175221 1 0.02 0
however, found growing things dynamically in loop inefficient , slow in r. new r, found there's dyplr or tidyverse use. hard me, don't know how handle weight column.
my try here:
final <- df %>% group_by(group) %>% nest() %>% mutate(data=map(data,~c(apply(.x,1,c)))) %>% mutate(data=map(data,~graph(.x,directed=f))) %>%
please show me how finish this?
Comments
Post a Comment