creating igraph with isolated nodes

断了今生、忘了曾经 提交于 2019-11-27 23:06:16

问题


I have a similiar problem to this one: Reading adjacency lists with isolated nodes using igraph

I want to plot nodes where some have no relationships. But for some reason the solution mentioned in the thread above is not working

my data

data <- data.frame(ID = c(143918,176206,210749,219170,247818,314764,321459,335945,339637,700689,712607,712946,735907,735907,735907,735907,735907,735907,735908,735908,735908,735908,735908,735908,735910,735911,735912,735913,746929,746929,747540,755003,767168,775558,776656,794173,794175,807493), relation = c(111098,210749,176206,NA,NA,NA,NA,NA,NA,807493,NA,NA,735908,735910,735911,735912,735913,767168,735907,735910,735911,735912,735913,767168,NA,NA,NA,NA,NA,100723,NA,NA,NA,776656,775558,NA,NA,700689))

This should result in a plot that also shows isolated nodes:

v<-unique(data[,1])
e <- na.omit(data)

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

For some reason I get the error: "Some vertex names in edge list are not listed in vertex data frame".

I hope someone can tell me what I am doing wrong and how to fix this. Thanks

EDIT: paqmo answers my question, thank you!

However my task requires a different approach. IDs that are in relations, but are missing in the first row, are people in a different location. Those should be omitted, while maintaining every isolated person from the first row. My solution for this uses data.table for now:

v <- unique(c(data[,1]))
v <- as.data.frame(v)
e <- data
setDT(v);setDT(e)
setkey(v)
setkey(e, relation)
e <- e[v]
e <- na.omit(e)
g<-graph.data.frame(e, vertices = v, directed = T)
plot(g)

any advice for a better/more efficient solution would be welcome.


回答1:


You need to define your vertex list based on both columns of your object data. Some vertices are in column 1, some in column 2. You are missing those in column 2.

You can check this with %in%:

> c(e[,1], e[,2]) %in% v
 [1]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
[19]  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
[37] FALSE  TRUE  TRUE  TRUE

As you can see, there are 2 elements of e that are not in v. Thus you get the error message that says as much.

Create the vertex list v by taking the unique values of both columns in data, less the NAs.

data <- data.frame(ID = c(143918,176206,210749,219170,
                          247818,314764,321459,335945,
                          339637,700689,712607,712946,
                          735907,735907,735907,735907,
                          735907,735907,735908,735908,
                          735908,735908,735908,735908,
                          735910,735911,735912,735913,
                          746929,746929,747540,755003,
                          767168,775558,776656,794173,
                          794175,807493), 
                   relation = c(111098,210749,176206,
                                NA,NA,NA,NA,NA,NA,807493,
                                NA,NA,735908,735910,735911,
                                735912,735913,767168,735907,
                                735910,735911,735912,735913,
                                767168,NA,NA,NA,NA,NA,100723,
                                NA,NA,NA,776656,775558,NA,NA,700689))

v <- unique(c(data[,1], data[,2])) #Define v from both columns in data
v <- na.omit(v)
e <- na.omit(data)

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




回答2:


It looks like you are trying to provide vertex name twice, i.e. once for the first column in e and then again as an argument, vertices = v.

Perhaps what you're really looking for is just

g <- graph.data.frame(e, directed = T)
plot(g)



来源:https://stackoverflow.com/questions/42673949/creating-igraph-with-isolated-nodes

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