Importing a Matrix

Step 1: Load igraph (of course)

library(igraph) 

Step 2: Read in the Matrix

Start with a csv file, since they are easy to import.

Initial.matrix <- read.csv(file.choose(), header=TRUE, row.names=1, check.names=FALSE, na.strings = "")

Step 3: Convert Missings (NA) to 0

This is optional. If you have created a network with only “1” to indicate the presence of a tie, but no “0” to indicate the absence, then you will need this.

Initial.matrix[is.na(Initial.matrix)] <- 0

Step 4: Convert to a Network

matrix <- as.matrix(Initial.matrix) 
g <- graph.adjacency(matrix, mode="directed", weighted=NULL) # For directed networks

g <- graph.adjacency(matrix, mode="undirected", weighted=NULL) # For undirected networks

You can now take a look at the network to see if it imported correctly.

g 
## IGRAPH cd8bad0 DN-- 25 165 -- 
## + attr: name (v/c)
## + edges from cd8bad0 (vertex names):
##   [1] a->b a->c a->f b->a b->c b->d b->g c->c c->h d->b d->e d->g d->i d->j
##  [15] d->o d->q d->r e->a e->d e->i e->q f->b f->c f->e f->h f->k f->p f->s
##  [29] g->g g->j g->o g->r h->b h->d h->h h->i h->k h->p h->q h->s i->a i->c
##  [43] i->e j->a j->b j->h j->j j->p j->r j->x k->a k->c k->d k->f k->g k->i
##  [57] k->k k->o k->q k->s k->x k->y l->b l->c l->d l->g l->h l->o l->p l->u
##  [71] l->x m->g m->i m->j m->l m->o m->q m->r m->t m->y n->b n->e n->h n->i
##  [85] n->j n->p n->q n->r o->a o->b o->c o->d o->y p->e p->h p->k p->p p->s
##  [99] p->x p->y q->a q->c q->d q->g q->h q->i q->j q->o q->p q->q q->r r->a
## + ... omitted several edges

You are doing a network analysis, so you may as well visualize it.

plot(g)

If the arrowheads are too large, try making them smaller.

plot.igraph(g, edge.arrow.size=0.2)

Keep in mind that the data used for this network were just randomly assigned. I made it quickly, so you may notice loops in the visualization.