Search code examples
rnetwork-programmingigraphbipartite

How do you count the number of vertices of a given class in an igraph object in R?


I'm working with a bipartite multilayer network in the igraph package for R.

Is there a way to count the number of vertices of a given class and the number of edges on a given layer?

The summary function gives me total counts and lists.

Here are the attributes of my network:

IGRAPH 3e83b45 UNWB 501 1120 -- + attr: name (v/c), taxon (v/c), taxon.label (v/n), species.size (v/n), type (v/c), weight (e/n), type (e/c) + edges from 3e83b45 (vertex names):

Vertex classes are coded as "taxon", layers (i.e., edge types) are coded as "type".

Thank you very much!


Solution

  • It'd be nice if you provided a minimal reproducible example, but I think simply querying the vertices with V() and querying the edges with E() will get you what you want. They both provide a vector that you can use the length() function on

    library(igraph)
    
    g <- make_graph('zachary') %>%
      set_edge_attr(., 'type', value = sample(c('parent', 'child'),
                                              size = ecount(.), 
                                              replace = T)) %>%
      set_vertex_attr(., 'taxon', value = sample(c('species', 'family', 'class'), 
                                                 size = vcount(.), 
                                                 replace = T))
    
    V(g)[taxon == 'species']
    E(g)[type == 'parent']