[英]Reading adjacency lists with isolated nodes using igraph
I would like to use igraph to explore some network data. 我想使用igraph探索一些网络数据。 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 第一列:焦点主题ID从2到4列:焦点主题的接收者
In the plot, subject 5 should be isolated. 在该图中,主题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)
As expected, NA appears as a node. 不出所料,NA似乎是一个节点。 Any ideas on how to deal with this? 有关如何处理此问题的任何想法?
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.