sdaza
sdaza

Reputation: 1052

Reading adjacency lists with isolated nodes using igraph

I would like to use igraph to explore some network data. My data have this structure:

a <- c(13, 32, NA, NA)
b <- c(32, NA, NA, NA)
c <- c(34, 13, 32, NA)
d <- c(5, NA, NA, NA)

net <- rbind(a, b, c, d)

First column: focal subject id From 2 to 4 columns: receivers from focal subject

In the plot, subject 5 should be isolated.

library(reshape)
library(igraph)

net <- as.data.frame(net)
mdata <- melt(net, id=c("V1"))
g <- graph.data.frame(mdata[,c(1,3)])  

Warning message:
In graph.data.frame(mdata[, c(1, 3)]) :
In `d' `NA' elements were replaced with string "NA"  

plot(g)

enter image description here

As expected, NA appears as a node. Any ideas on how to deal with this?

Upvotes: 3

Views: 1267

Answers (1)

sdaza
sdaza

Reputation: 1052

I had to define vertices and edges separately:

v <- unique(net[, 1])
mdata <- melt(net, id=c("V1"))
e <- na.omit(mdata[,c(1,3)])

g <- graph.data.frame(e, vertices=v, directed=TRUE)
plot(g)

enter image description here

Upvotes: 2

Related Questions