簡體   English   中英

如何將值向量分配給 R 中 igraph 中的頂點 label?

[英]How to assign a vector of values to a vertex label in igraph in R?

假設,我在igraph中有一個包含 3 個節點和 3 條邊的圖,如下所示:

library(igraph)
G <- graph(c(1,2,1,3,2,3),directed = FALSE)
V(G)$myLabel <- 1:3

它工作得很好。 但我想將 label 分配給每個具有不同長度的節點。 例如:

G <- graph(c(1,2,1,3,2,3),directed = FALSE)
V(G)[1]$myLabel <- c(10,20)
V(G)[2]$myLabel <- c(-1,-2,-3)
V(G)[3]$myLabel <- c(100,200,300,400)

在我對圖表的分析過程中,每個節點的標簽長度可能會發生變化。 我怎樣才能做到這一點?

您必須將長度為 1 的對象分配給節點。 否則,只取第一個元素。 但是,您可以使用list來封裝向量以滿足該要求:

library(igraph)
#> 
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:stats':
#> 
#>     decompose, spectrum
#> The following object is masked from 'package:base':
#> 
#>     union

G <- graph(c(1,2,1,3,2,3),directed = FALSE)

V(G)[1]$myLabel <- list(c(10,20))
V(G)[2]$myLabel <- list(c(-1,-2,-3))
V(G)[3]$myLabel <- list(c(100,200,300,400))

V(G)[1]$myLabel
#> [[1]]
#> [1] 10 20
V(G)[2]$myLabel
#> [[1]]
#> [1] -1 -2 -3
V(G)[3]$myLabel
#> [[1]]
#> [1] 100 200 300 400

# adding a new label to one node
V(G)[3]$myLabel <- list(c(V(G)[3]$myLabel[[1]], 500))
V(G)[3]$myLabel
#> [[1]]
#> [1] 100 200 300 400 500

代表 package (v2.0.1) 於 2021 年 12 月 13 日創建

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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