Learning igraph with R
I’m an old programmer bigot, so doing things in R is always a bit weird (I’m still not used to “.” having no meaning). This post will (hopefully) help me later when I have to do this again. It’s actually the third time I have to relearn igraph on R =/
Load a graph
Knowing a little R, I expect that I can load a graph in memory with just one line.
I little googling and http://igraph.org/r/doc/read_graph.html:
The
read_graphfunction is able to read graphs in various representations from a file, or from a http connection.
Great! How about the file format? Hmmm… edge list? Not clear what’s expected but it seems something simple like:
node1 node2
node2 node3
I wonder if it accepts weight and direction, let’s try:
node1 node2 9
node2 node3 99
G <- read_graph(“my_file.edge”, directed = “true”)
Nah… let’s remove the weight…
node1 node2
node2 node3
Parse error again =(
Okay, let’s find something else. After some more googling I find tutorials here and there reading things from csv files.
https://gist.github.com/ruliana/d32d022c389d08c0be8b042075281f9f
loading a directed weighted graph
I learned that if you name a column “weight”, the function “graph.data-frame” do the right thing. Not bad, it actually took 2 lines, but that’s fair enough.
Let me try with my 8.000 nodes graph… fast and easy. Very good!
Network descriptives
I’m interest in network descriptives, let’s try to find the most central node by degree.
The function “centr_degree(G)” looks very promising, but now I have a matrix of node degrees, how to find the biggest one?
Hmm… “degree(G)” looks better, I just need to sort it and take some at the beginning.
https://gist.github.com/ruliana/5d0078a1a579739ae07aeb22cb48dca8
Eeeeeasy! We also have “strength”, which is the sum of weights.
https://gist.github.com/ruliana/bcb40ddb1b5327ecfea7adc8514936d7
Not bad at all =)
Let’s try reciprocity, as it matter a lot to my research… Reciprocity is zero, that’s ok… wait… what?! It can’t be zero o_O
Ohhh… let me take some time to read the docs. Hmmm… hmmm… not helping. Maybe it thinks my graph is undirected? Let’s test with a smaller data set. Worked.
Looking for a solution I find the useful “is.mutual(G)”:
https://gist.github.com/ruliana/68a3dd48bf12c109a6a17a28c1edde1f
But my big graph still returns nothing…
Some hours googling and looking source codes and still nothing, must be something really obvious.
I find it!!! And, at this right moment, I hate “read.csv” >_< My csv file had a single space after each comma, even using quotes for every column, the parse ignores it and add a space before the string values. Ugh… living and learning.
https://gist.github.com/ruliana/f8c4762999c16273d4ea43ab20bdd2c6
I think that’s enough for now. Next time I’m going to try some more graph functions.
So far, R and igraph are really easy to work with, but for some minor inconveniences.