简体   繁体   中英

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM