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:
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:
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…
Parse error again =(
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!
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.
Eeeeeasy! We also have “strength”, which is the sum of weights.
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)”:
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.
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.