我正在使用R中的igraph与一个巨大的网络,我有点害怕搞砸了df。我关注了Vertex/node attributes for igraph objects并阅读了R-igraph教程和文档。
然而,我错过了一些东西,假设我有这些数据:
toy_data = data.table(source = c(1,1,1,3,5,5,1,1,1,3,5,5),
source_name=c(Milan,Milan,Milan,Frankfurt,London,London,Milan,Milan,Milan,Frankfurt,London,London),
from=c("A","A","A","C","E","E","A","A","A","C","E","E"),
target=c(2,3,1,4,6,5,5,1,1,1,3,NA), target_name=c(Paris,3,1,4,6,5,5,1,1,1,3,NA),
to=c("B","C","A","D","F","E","E","A","A","A","C",NA))
edges <- toy_data[,.(source,target)]
v <- data.frame(labels=as.character(unique(unlist(toy_data[,.(source,target)]))),
names = as.character(unique(unlist(toy_data[,.(source_name,target_name)]))),
category = as.character(unique(unlist(toy_data[,.(from,to)]))))
graph <- graph_from_data_frame(edges, vertices = v, directed = FALSE)
plot(graph,vertex.label=v$names,vertex.color=c("pink","skyblue")[1+(V(graph)$category=="A")])
只要未列出的“唯一”向量具有相同的长度,所有这些都很好,但对我来说,将顶点属性单独加载为单独的列似乎不是一个很好的做法,因为有一个重复就足够了(这里的from和to字段具有“A”而不是“C”),这些向量不再大小相等:
toy_data = data.table(source = c(1,1,1,3,5,5,1,1,1,3,5,5),
source_name= c("Milan","Milan","Milan","Frankfurt","London","London","Milan","Milan","Milan","Frankfurt","London","London"),
from=c("A","A","A","A","E","E","A","A","A","A","E","E"),
target=c(2,3,1,4,6,5,5,1,1,1,3,NA),
target_name=c("Paris","Frankfurt","Milan","Dublin","Madrid","London","London","Milan","Milan","Milan","Frankfurt",NA),
to=c("B","A","A","D","F","E","E","A","A","A","A",NA))
toy_data
edges <- toy_data[,.(source,target)]
v <- data.frame(labels=as.character(unique(unlist(toy_data[,.(source,target)]))),
names = as.character(unique(unlist(toy_data[,.(source_name,target_name)]))),
category = as.character(unique(unlist(toy_data[,.(from,to)]))))
graph <- graph_from_data_frame(edges, vertices = v, directed = FALSE)
plot(graph,vertex.label=v$names,vertex.color=c("pink","skyblue")[1+(V(graph)$category=="A")])
因此,如果我有一个以这种方式构造的data.table,我如何告诉igraph
将节点id绑定到某些特性?python中的zip()函数)
1条答案
按热度按时间8cdiaqws1#
提醒一下,我自己还在试着弄明白igraph。但是,看起来Source和Target字段可以重命名为公共属性。与创建边表类似,可以创建具有唯一ID和属性的顶点表。然后可以在图上使用
set_vertex_attr
。另外,注意
NA
被列为节点,并且没有被处理。考虑处理,否则igraph将抛出错误。