7

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

  1. degree(my.graph)==0
  2. the name of the vertex begins with a specified character.

Here is an example showing what I want to get. From the following graph:

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

I want to remove vertices with degree 0 that start with 5. In this case I want to remove only vertex 588 (but not 490 and 587). I know how to remove vertices starting with 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 put these two conditions together, 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 get back the full graph. Is there a special way of combining multiple conditions for removing vertices?

Thank you!

Justyna
  • 737
  • 2
  • 10
  • 25
  • Use `&`, not `&&`. (See `?Logic` help page for the difference) – MrFlick Apr 21 '15 at 22:14
  • I just figured it out too. I was using wrong operator for and. Thank you! – Justyna Apr 21 '15 at 22:20
  • So, when you plot it with just `&` instead of `&&` you get what you want? For me, that leaves 490 in the graph, and I thought you wanted to get rid of 490 since it has a degree of 0. – Jota Apr 21 '15 at 22:33
  • I wanted to get rid of only those nodes that satisfy both conditions, that is 490 should stay because of the name. `&` does exactly that. – Justyna Apr 22 '15 at 11:21

1 Answers1

8

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 :)

Michał
  • 2,755
  • 1
  • 17
  • 20