Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to subset a large igraph to just one node and its connected nodes using R?

Tags:

r

igraph

I have a very large igraph (called g) in R.

I am just interested in one node (NodeA) plus whatever nodes are directly connected to NodeA. I want to end up with a very simple graph like the picture below.

I have tried subgraph(g, "nodeA") but I just end up with NodeA all by itself.

I think I need to subset the edges of graph g to those that connect to nodeA then use subgraph.edges(). I can't figure out how to subset the edges based on what nodes they are connected to...

NodeA and two directly connected nodes

like image 727
Mel Avatar asked Nov 08 '22 08:11

Mel


1 Answers

Try using neighbors()

# Example graph
g1 <- graph_from_literal(1:2:3---3:4:5)

# Let's take a look at it
plot(g1)

enter image description here

# Say we are interested in the subgraph consisting of vertex 1 and all
# other vertices it is connected to

g2 <- induced_subgraph(g1, c(1, neighbors(g1,1)))
plot(g2)

enter image description here

like image 145
Marcus Campbell Avatar answered Nov 14 '22 21:11

Marcus Campbell