Conditional removal of vertices based on attributes in r

I am working with a graph that has 121 vertices and 209 edges, and I'm trying to remove from these vertices a graph that satisfy two conditions:

  • degree(my.graph)==0
  • the vertex name begins with the specified character.

Here is an example showing what I want to get. In the following chart:

 toy.graph <- graph.formula(121-221,121-345,121-587,345-587,221-587, 490, 588) 

I want to delete vertices with degree 0 starting with 5. In this case, I want to delete only vertex 588 (but not 490 and 587). I know how to remove vertices starting at 5:

 delete.vertices(toy.graph,V(toy.graph)$name %in% grep("^5",V(toy.graph)$name,value=T)) 

and how to remove vertices with degree 0:

 delete.vertices(toy.graph, V(toy.graph)[degree(toy.graph)==0]) 

but when I try to combine these two conditions, that is

 delete.vertices(toy.graph, V(toy.graph)$name %in% grep("^5",V(toy.graph)$name,value=T) && V(toy.graph)[degree(toy.graph)==0]) 

it does not work, and I return the full schedule. Is there a special way to combine multiple conditions to remove vertices?

Thanks!

+5
source share
1 answer

I believe this is what you want:

 delete.vertices(toy.graph, V(toy.graph)[ degree(toy.graph) == 0 & grepl("^5", V(toy.graph)$name) ] ) 

pozdrawiam :)

+4
source

All Articles