1

I am looking for a way to subgraph edges based on a vertex attribute score of at least one of the vertices that are incident on that edge.

Is there an easy way to do so?

Any suggestions?

wake_wake
  • 1,332
  • 2
  • 19
  • 46

1 Answers1

11

This question is missing a reproducible example or any example data of any kind. I'm going to take a risk and answer based on what I can guess Is being asked. First, I'll create a sample graph and assign arbitrary "prop" values to each vertex. I'll color them assuming i'm interested in prop>=3 and plot them.

library(igraph)

gg <- graph.atlas(711)
V(gg)$name <- 1:7
V(gg)$prop <- c(1,2,2,3,3,1,1)
V(gg)$color <- ifelse(V(gg)$prop>=3, "orange","yellow")
plot(gg)

full graph

Now, I can find all the edges connected to a vertex with prop>=3 with

E(gg)[inc(V(gg)[prop>=3])]
# Edge sequence:
#            
# [3]  4 -- 3
# [4]  5 -- 4
# [5]  6 -- 5
# [10] 5 -- 3

And if I like I can extract those to a sub-graph with

g2 <- subgraph.edges(gg, E(gg)[inc(V(gg)[prop>=3])])
plot(g2)

sub-graph

Community
  • 1
  • 1
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • 1
    Thanks MrFlick! This is exactly what I was looking for. I have never used the 'inc' function. Big thanks! – wake_wake Aug 20 '14 at 22:17
  • 1
    Sure. Just in the future, try to include a minimal reproducible example. `graph.atlas` or `graph.famous` can be helpful for this. – MrFlick Aug 20 '14 at 22:21
  • Is there a way I can alter the code in such a way that it does not also select the edges on which ALL vertices satisfy `prop>=3`? I want to select the patents on which at least one but not all satisfy `prop>=3'. Do you know a way? – wake_wake Aug 21 '14 at 10:03
  • 1
    @Frank-LA That is a bit of a different problem. Comments don't give me much room to explain but `ev<-get.edges(gg, E(gg)); exce<-xor(V(gg)[ev[,1]]$prop>=3, V(gg)[ev[,2]]$prop>=3); subgraph.edges(gg, E(gg)[exce])` should work for this sample. It basically leaves off the 4-5 edges. – MrFlick Aug 21 '14 at 16:36
  • Thanks MrFlick. This works great in this example. But in my real data, I have patents with up to 31 inventors. Is there a way that I can set the restrictions: select if at least one meets criteria but don't select if all meet criteria? Maybe I should open a new question? – wake_wake Aug 22 '14 at 10:13
  • Props for introducing me to the `inc` function. The answer helped me to think through and solve my related problem. – timothyjgraham May 18 '17 at 19:59