[英]creating igraph with isolated nodes
I have a similiar problem to this one: Reading adjacency lists with isolated nodes using igraph 我对此有一个类似的问题: 使用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! 编辑:paqmo回答了我的问题,谢谢!
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. 关系中但第一行中缺少的ID是位于不同位置的人员。 Those should be omitted, while maintaining every isolated person from the first row. 在保留第一行中每个孤立的人的同时,应该省略这些内容。 My solution for this uses data.table for now: 我为此的解决方案现在使用data.table:
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. 任何关于更好/更有效解决方案的建议都将受到欢迎。
You need to define your vertex list based on both columns of your object data
. 您需要根据对象data
两列定义顶点列表。 Some vertices are in column 1, some in column 2. You are missing those in column 2. 在第1列中有一些顶点,在第2列中有一些顶点。在第2列中缺少那些顶点。
You can check this with %in%
: 您可以使用%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
. 如您所见, e
中有2个元素不在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
中两列的唯一值(减去NA)来创建顶点列表v
。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.