Loading Network Data from a File

When get network data from an archival source, and it will be in the form of a matrix or an edge list, typically in some kind of comma separated value (csv) format. Here will show how to input that into R to create an igraph network object from an outside file.

First we will write the Pulp Fiction data into an edge list and save it to your local folder as a csv file.

First, let’s load the data and transform it into an edgelist:

   library(igraph)
   library(networkdata)
   g <- movie_559 #pulp fiction data
   #install.packages(here)
   library(here)
   g.el <- as_edgelist(g) #transforming graph to edgelist
   names(g.el) <- c("name1", "name2")

Now, let’s write it into disk:

   write.csv(g.el, here("pulp.csv"))

The write.csv function just saves an R object into a .csv file. Here the R object is “g.el” and we asked it to save just the columns which contain the name of each character. This represents the adjacency relations in the network as an edge list. We use the package here to keep track of our working directory. See here (pun intended) for details.

Now suppose that’s the network we want to work with and it’s saved in our hard drive. To load it, we just type:

   g.el <- read.csv(here("pulp.csv"), 
                    col.names = c("name1", "name2"))
   head(g.el)
  name1     name2
1 BRETT MARSELLUS
2 BRETT    MARVIN
3 BRETT     ROGER
4 BRETT   VINCENT
5 BUDDY       MIA
6 BUDDY   VINCENT

Which gives us the edge list we want now saved into an R object of class data.frame. So all we need is to convert that into an igraph object. To do that we use one of the many graph_from... functions in the igraph package. In this case, we want graph_from_edgelist because our network is stored as an edge list:

   g.el <- as.matrix(g.el)
   g <- graph_from_edgelist(g.el, directed = FALSE)
   V(g)
+ 38/38 vertices, named, from acb163f:
 [1] BRETT           MARSELLUS       MARVIN          ROGER          
 [5] VINCENT         BUDDY           MIA             BUTCH          
 [9] CAPT KOONS      ESMARELDA       GAWKER #2       JULES          
[13] PEDESTRIAN      SPORTSCASTER #1 ENGLISH DAVE    FABIENNE       
[17] FOURTH MAN      HONEY BUNNY     MANAGER         JIMMIE         
[21] JODY            PATRON          PUMPKIN         RAQUEL         
[25] WINSTON         LANCE           MAYNARD         THE GIMP       
[29] ZED             ED SULLIVAN     MOTHER          WOMAN          
[33] PREACHER        SPORTSCASTER #2 THE WOLF        WAITRESS       
[37] YOUNG MAN       YOUNG WOMAN    
   E(g)
+ 102/102 edges from acb163f (vertex names):
 [1] BRETT      --MARSELLUS       BRETT      --MARVIN         
 [3] BRETT      --ROGER           BRETT      --VINCENT        
 [5] BUDDY      --MIA             VINCENT    --BUDDY          
 [7] BRETT      --BUTCH           BUTCH      --CAPT KOONS     
 [9] BUTCH      --ESMARELDA       BUTCH      --GAWKER #2      
[11] BUTCH      --JULES           MARSELLUS  --BUTCH          
[13] BUTCH      --PEDESTRIAN      BUTCH      --SPORTSCASTER #1
[15] BUTCH      --ENGLISH DAVE    BRETT      --FABIENNE       
[17] BUTCH      --FABIENNE        JULES      --FABIENNE       
[19] JULES      --FOURTH MAN      VINCENT    --FOURTH MAN     
+ ... omitted several edges

Which gives us back the original igraph object. Note that we specified that the graph is undirected by setting the option directed to false.