Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In R, how can I generate a subgraph from a igraph object based on multiple attribute scores?

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!

like image 866
wake_wake Avatar asked Jun 12 '14 17:06

wake_wake


2 Answers

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") ])
like image 74
Gabor Csardi Avatar answered Sep 28 '22 09:09

Gabor Csardi


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")))
like image 43
MrFlick Avatar answered Sep 28 '22 09:09

MrFlick