I have a network graph "G" based on the following edges:
library(igraph)
edges <- data.frame(
from=c(1,1,4,4,4,5,5,6),
to= c(2,3,5,6,7,6,7,7))
G <- graph_from_data_frame(d=edges, directed=F)
This example clearly contains 2 subnetworks, the first with nodes 1,2,3 and the second one with nodes 4,5,6,7. I would like to:
Thus, in this example, the function will ideally create an object with as many rows as number of nodes in G, and two columns: the first contains a vector that indicatse the ID of the subnetwork and the second with the size (gsize) of the subnetwork. .
result <- data.frame(
ID=c(1,1,2,2,2,2,2,2),
gsize=c(3,3,3,4,4,4,4,4))
G <- graph_from_data_frame(d=edges, directed=F)
I am new using igraph
so maybe there is a function to do this.
If it's just disconnected components you are interested in:
library(igraph) library(dplyr) edges <- data.frame( from=c(1,1,4,4,4,5,5,6), to= c(2,3,5,6,7,6,7,7)) G <- igraph::graph_from_data_frame(d=edges, directed=F) # create requested dataframe df <- data.frame(node_ID = as.vector(V(G)), community = as.vector(components(G)$membership)) required_df <- df %>% dplyr::inner_join(df %>% dplyr::group_by(community) %>% dplyr::count(name = "community_size") )
If it is more complex clusters you are interested in:
library(igraph) library(dplyr) edges <- data.frame( from=c(1,1,4,4,4,5,5,6), to= c(2,3,5,6,7,6,7,7)) G <- igraph::graph_from_data_frame(d=edges, directed=F) # find subnetworks using louvain algorithm and adding to community in graph louvain_partition <- igraph::cluster_louvain(G) G$community <- louvain_partition$membership # create requested dataframe df <- data.frame(node_ID = as.vector(V(G)), community = G$community) required_df <- df %>% dplyr::inner_join(df %>% dplyr::group_by(community) %>% dplyr::count(name = "community_size") )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.