3

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?

gunr2171
  • 16,104
  • 25
  • 61
  • 88
sdaza
  • 1,032
  • 13
  • 29

1 Answers1

2

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

gunr2171
  • 16,104
  • 25
  • 61
  • 88
sdaza
  • 1,032
  • 13
  • 29