简体   繁体   English

节点属性csv图

[英]node attribute csv igraph

I have a large number of adjacency matrices, in csv format exported from excel. 我有大量从Excel导出的csv格式的邻接矩阵。 I also have a large number of csv. 我也有大量的csv。 files with vertex attribute data. 具有顶点属性数据的文件。 I have linked them in SNA but igraph goes further functionally, so I am looking to move to it, but I am failing to be able to build the graph+attribute files. 我已经在SNA中链接了它们,但是igraph在功能上走得更远,因此我希望继续使用它,但无法构建graph + attribute文件。

I am looking to set up some code that will be a workhorse for doing a range of plots. 我正在寻找设置一些代码,这些代码将成为进行一系列绘图的主要工具。 Although there seem many ways to link these two data sets it seemed this was the simplest: To make the adjacency matrix in the csv a data frame (cut down for missing vertex data) I use: 尽管似乎有很多方法可以链接这两个数据集,但这似乎是最简单的:为了使csv中的邻接矩阵成为数据帧(针对丢失的顶点数据而减少),我使用:

m <- read.table(header=TRUE, check.names=FALSE, textConnection("
      2 3 4 5 6 7 
    2 0 1 1 0 1 0 
    3 1 0 0 0 1 0 
    4 0 0 0 0 0 0 
    5 1 0 1 0 0 1 
    6 0 0 0 0 0 0 
    7 1 1 0 1 0 0 
"))

In the case of having both vertex and row names in the original file, the imported attributes file has both vertex names and 'row.names' which correspond to the node names. 在原始文件中同时具有顶点名称和行名称的情况下,导入的属性文件同时具有顶点名称和与节点名称相对应的“ row.names”。 Hex.ed[1,1] gives the value of the attribute for the first node in the m network, ie node 2: Hex.ed[1,1]给出m网络中第一个节点(即节点2)的属性值:

Hex.ed <- read.table(header=TRUE, textConnection("
        HH    Emo  Extra   Aggr Consci    OTE
  2 3.3750 3.0000 3.0000 3.0000 3.0625 3.4375
  3 3.5625 2.9375 3.0625 3.0000 3.3125 3.6250
  4 3.2500 2.8750 3.7500 3.2500 3.8750 3.5000
  5 3.6875 3.1250 3.3750 3.5625 3.6250 3.3125
  6 3.3125 3.0000 3.3125 3.8750 3.2500 3.6875
  7 3.8125 3.2500 3.5625 2.8750 3.6875 3.4375
"))

g <- graph.data.frame(m, directed=TRUE, vertices=Hex.ed)

However, I get the error: Error in graph.data.frame(m, directed = TRUE, vertices = Hex.ed) : Duplicate vertex names 但是,我得到了错误: Error in graph.data.frame(m, directed = TRUE, vertices = Hex.ed) : Duplicate vertex names

I get a different error message: 我收到不同的错误消息:

Error in graph.data.frame(m, directed = TRUE, vertices = Hex.ed) : 
  Some vertex names in edge list are not listed in vertex data frame

but this is because you were not running the example in the question, but used your complete data set, possibly. 但这是因为您没有运行问题中的示例,而是可能使用了完整的数据集。

Anyway, graph.data.frame does not use adjacency matrices. 无论如何, graph.data.frame不使用邻接矩阵。 From the docs at http://igraph.sourceforge.net/doc/R/graph.data.frame.html : 从位于http://igraph.sourceforge.net/doc/R/graph.data.frame.html的文档中:

... the first two columns of d are used as a symbolic edge list and additional columns as edge attributes. ... d的前两列用作符号边缘列表,其他列用作边缘属性。 The names of the attributes are taken from the names of the columns. 属性的名称取自列的名称。

If you cared about reading the manual you would have seen an example at the bottom. 如果您关心阅读手册,那么您将在底部看到一个示例。

If you have an adjacency matrix, then you can use graph.adjacency to create the graph, and then add the vertex attribute one by one: 如果具有邻接矩阵,则可以使用graph.adjacency创建图,然后一个一个地添加顶点属性:

g <- graph.adjacency(as.matrix(m))
for (i in seq_len(ncol(Hex.ed))) {
  g <- set.vertex.attribute(g, colnames(Hex.ed)[i], value=Hex.ed[,i])
}
g
# IGRAPH DN-- 6 11 -- 
# + attr: name (v/c), HH (v/n), Emo (v/n), Extra (v/n), Aggr (v/n),
#   Consci (v/n), OTE (v/n)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM