Reading adjacency lists with isolated nodes using igraph

本秂侑毒 提交于 2019-12-11 02:51:16

问题


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)

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


回答1:


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)



来源:https://stackoverflow.com/questions/23974256/reading-adjacency-lists-with-isolated-nodes-using-igraph

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!