Search code examples
rfilterigraphdata-manipulationadjacency-matrix

filter igraph object by vertex attribute value


mymatrix is an igraph object (weighted adjacency matrix):

> summary(mymatrix)
IGRAPH 14317c4 UNW- 810 8761 -- 
+ attr: name (v/c), year (v/n), bad (v/n), new (v/n), weight (e/n)

One of the node attributes is year:

> summary(V(mymatrix)$year)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   1990    2011    2014    2013    2017    2019 

newmatrix is an igraph object that only includes nodes associated with a given year. If the value of year is greater than 2013, I want to remove the node from both the row and column of the matrix prior to graphing and analysis.

I tried to use filter() in the dplyr package and get the following error:

> newmatrix<-filter(mymatrix, V(mymatrix)$year<2013)
Error in UseMethod("filter_") : 
  no applicable method for 'filter_' applied to an object of class "igraph"

One work-around is modifying the data I used to produce the adjacency matrix in the first place. But I wanted to know if there was a way to filter igraph objects by node attributes that I haven't found in the documentation.


Solution

  • Looks like

    delete_vertices(mymatrix, V(mymatrix)$year >= 2013)
    

    should give the expected graph.