1

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:

  1. Identify to which subnetwork node "i" belongs to.
  2. The number of nodes in each subnetwork.

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.

user20650
  • 24,654
  • 5
  • 56
  • 91

1 Answers1

1

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")
  )
Keith McNulty
  • 952
  • 1
  • 6
  • 17
  • 1
    re you edit: you can grab all the info from components without using joins: `with(components(G), data.frame(node = names(membership), component = membership, size=csize[membership]))` – user20650 Apr 03 '20 at 12:05
  • Thanks a lot Keith ! The components function was what I was searching for. – Iván Tzintzun Apr 05 '20 at 11:21