0

I am trying to count those vertices with only incoming or outgoing edges based on a graph made with igraph package in R.

This is my piece of code in R using igraph:

library(igraph)
web <- read.csv(file = "myweb.csv", header = T) 
g=graph.data.frame(web) 

Any advice on how to do this would be much appreciated.

Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
Tomas Marina
  • 73
  • 10
  • When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Avoid using `read.csv` with files we don't have when asking for help. – MrFlick Apr 24 '18 at 14:14

1 Answers1

4

You can determine how many incoming or outgoing edges a vertex has with the degree() function using the argument mode = 'in' or mode='out'. Below is some sample code that computes the number (and which) nodes have both incoming and outgoing edges:

set.seed(123)
df <- data.frame(from = sample(1:20, 10), to = sample(1:20, 10))
library(igraph)
g <- graph.data.frame(df)
plot(g)

# Vertices with both incoming and outgoing links
V(g)[degree(g, mode = 'out')>0 & degree(g, mode = 'in') > 0]
#> + 3/17 vertices, named, from a36c786:
#> [1] 15 1  20
# number of vertices
length(V(g)[degree(g, mode = 'out')>0 & degree(g, mode = 'in') > 0])
#> [1] 3

# Number of vertices with outgoing links
length(V(g)[degree(g, mode = 'out')>0])
#> [1] 10

# Number of vertices with incoming links
length(V(g)[degree(g, mode = 'in')>0])
#> [1] 10
gfgm
  • 3,627
  • 14
  • 34
  • that is exactly what I was looking for. Thank you for your time and expertise! – Tomas Marina Apr 24 '18 at 15:20
  • @TomasMarina glad it helped. If I answered your question you can accept the answer (click the checkmark on the left side of the question), and if you found it helpful you can upvote it (click the up arrow). – gfgm Apr 24 '18 at 15:28