Choosing vertices by edge weight

218 views Asked by At

I have a network with two types of edges, one with edge$weight=1 and the other edge$weight=2 (to define different relationships e.g. marriage vs. kinship). I want to choose the neighbors of nodes that are linked by edge$weight=2. Then, after getting the vertex names of the neighbors, I will calculate their degree only for edge$weight=1.

It seems simple, but the following code does not give the correct neighbor names:

V(net)[E(net)[E(net)$weight==2]]

Is there another way to get vertex names with respect to the edge type?

1

There are 1 answers

0
Gary Weissman On

I think you are looking for the adj function which will return the vertices adjacent to a given edge set. In your case you can choose all the relevant vertices by using V(net)[ adj(E(net)$weight==2) ]

For example:

# make a sample graph with edge weights 1 or 2
net <- graph.full(20)
E(net)$weight <- sample(1:2,ecount(net),replace=T)

# now select only those vertices connected by an edge with weight == 2
V(net)[ adj(E(net)$weight==2) ]