[英]Add attributes to graphs using igraph in r
I have a dataset of features in the form of a csv available at below link 我在下面的链接中有一个csv形式的功能数据集
https://github.com/pranavn91/PhD/blob/master/Expt/27022%20feat.csv https://github.com/pranavn91/PhD/blob/master/Expt/27022%20feat.csv
name birthday education classes.from.id
27136 6971 NA NA
27137 841 NA NA
27138 841 NA NA
27139 841 NA NA
I have an adjacency matrix of a graph in the form of a csv available at below link 我有一个csv形式的邻接矩阵,可以在下面的链接中找到
https://github.com/pranavn91/PhD/blob/master/Expt/27022.csv https://github.com/pranavn91/PhD/blob/master/Expt/27022.csv
27139 27138 27136 27137
27139 0 1 1 1
27138 1 0 0 0
27136 1 0 0 1
27137 1 0 1 0
I want to convert adjacency matrix to graph and set the vertex attributes. 我想将邻接矩阵转换为图形并设置顶点属性。 but when i check the birthday of first vertex of the graph, i get 841 ie value of the second node.
但是当我检查图的第一个顶点的生日时,我得到841即第二个节点的值。 Also in the edgelist the edges of first node are missing
同样在边缘列表中,缺少第一个节点的边缘
library(igraph)
path <- "https://raw.githubusercontent.com/pranavn91/PhD/master/Expt/27022.csv"
dat <- read.csv(path, row.names=1, check.names=FALSE, header=T)
m = as.matrix(dat)
g = graph.adjacency(m,mode="undirected",weighted=NULL)
path2 <- "https://raw.githubusercontent.com/pranavn91/PhD/master/Expt/27022%20feat.csv"
prop <- read.csv(path2,row.names=1, check.names=FALSE, header=T)
head(prop)
for (i in V(g)) {
for (j in names(prop)) {
g <- set.vertex.attribute(g,
j,
index = i,
prop[i + 1, j])
}
}
igraph::degree(g,v=V(g))
###degrees of nodes in g are 27136 - 3, 27137 - 1, 27138 - 2, 27139 - 2
###actual as per adjacency matrix should be 27136 - 2, 27137 - 2, 27138 - 1, 27139 - 3
The following works for me: 以下适用于我:
for (nm in names(attribs)) g <- set_vertex_attr(g, nm, value = attribs[[nm]])
V(g)$birthday
## [1] 6971 841 6971 841
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.