簡體   English   中英

使用r中的igraph將屬性添加到圖形

[英]Add attributes to graphs using igraph in r

我在下面的鏈接中有一個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

我有一個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

我想將鄰接矩陣轉換為圖形並設置頂點屬性。 但是當我檢查圖的第一個頂點的生日時,我得到841即第二個節點的值。 同樣在邊緣列表中,缺少第一個節點的邊緣

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

以下適用於我:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM