I have a igraph
object with 3000 vertices and 4000 edges. Both vertices and edges hold attributes.
One of the vertex attributes is city
and has a valid input for all vertices.
I want to select all vertices that live in the top 10 most common cities and create a new graph for these. I know what these top 10 cities are.
When I do so for a single city, it works fine:
new_graph<-induced.subgraph(old_graph, which(V(old_graph$city=="LOS ANGELES")
However, I do want to include 9 more cities into the new_graph
.
Can I simple extent my which
argument here? Or should I write a loop
?
Does anyone has some ideas? Any help will greatly be appreciated!
Maybe somewhat more readable, you can avoid the which
:
new_graph <- induced.subgraph(old_graph,
V(old_graph)[ city %in% c("LOS ANGELES", "BOSTON", "KALAMAZOO") ])
Rather than testing for equality, you can use the %in%
operator to match any value in a list. Just use
new_graph<-induced.subgraph(old_graph,
which(V(old_graph)$city %in% c("LOS ANGELES","BOSTON","KALAMAZOO")))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With