使用igraph读取具有孤立节点的邻接列表

时间:2014-05-31 20:20:10

标签: r igraph

我想用igraph来探索一些网络数据。我的数据有这样的结构:

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)

第一栏:焦点主题id 从2到4列:焦点主题的接收器

在图中,应隔离对象5。

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

正如预期的那样,NA显示为节点。关于如何处理这个的任何想法?

1 个答案:

答案 0 :(得分:1)

我必须分别定义顶点和边缘:

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

相关问题