rigraphsnasubgraph

Splitting a large network into many subgraphs using igraph


I have a large dataset comprising social network data from a number of different classrooms across different schools. I would like to split this large network into many smaller classroom networks according to a variable "class_id" or "school_id". I am looking for a function similar to split for standard dataframes, which allows you to specify a factor f defining the number of groups. I have considered using the function induced_subgraph from the igraph package. However, looking at the package documentation, the function only contains the option vids where one should list the vertices that will form the subgraph. Is there a way to automate this process (perhaps using lapply) and specify the variable that induces the grouping? Alternatively, is there any other function capable of doing this?


Solution

  • Something like this?

    library(igraph)
    
    net <- erdos.renyi.game(n= 100, p = .05) # generate random graph
    class_id <- sample(1:5, 100, replace = T) # generate random groups
    
    # create list of group memberships
    class_id_list <- lapply(unique(class_id), function(i,j){
                            which(j %in% i)},
                            j = class_id)
    
    # extract subnets based on group memberships
    split.net <- lapply(class_id_list, function(net, v){
      induced_subgraph(net, vids = v)
      },
      net = net)