R:更改特定顶点的顶点属性

R: Change vertex attribute of a particular vertex

我的网络如下所示:

> get.vertex.attribute(g)
$name
[1] "T1" "A1"  "A2"  "A3"  "A3" 

$color
[1] NA        "#D53E4F" "#D53E4F" "#F6E68E" "#EE6445"

$weight
[1] NA 34 15 11  6

我希望用黑色替换 V(g)$color 中的 NA 值,用数字替换 V(g)$weight。谁知道如何为特定顶点设置顶点值,尤其是具有 NA 顶点属性的顶点?

您可以使用set.vertex.attribute

get.vertex.attribute(g)
#> $name
#> [1] "T1" "A1" "A2" "A3" "A3"
#> 
#> $color
#> [1] NA        "#D53E4F" "#D53E4F" "#F6E68E" "#EE6445"
#> 
#> $weight
#> [1] NA 34 15 11  6

g <- set.vertex.attribute(g, "color", 1, "black")
g <- set.vertex.attribute(g, "weight", 1, 100)

get.vertex.attribute(g)
#> $name
#> [1] "T1" "A1" "A2" "A3" "A3"
#> 
#> $color
#> [1] "black"   "#D53E4F" "#D53E4F" "#F6E68E" "#EE6445"
#> 
#> $weight
#> [1] 100  34  15  11   6

reprex package (v2.0.1)

创建于 2022-04-26

数据

library(igraph)

g <- make_graph(~T1-A1-A2-A3-A4)
g <- set.vertex.attribute(g, "name", 1:5,  c("T1", "A1", "A2", "A3", "A3"))
g <- set.vertex.attribute(g, "color", 1:5,
                          c( NA, "#D53E4F", "#D53E4F", "#F6E68E", "#EE6445"))
g <- set.vertex.attribute(g, "weight", 1:5, c(NA, 34, 15, 11,  6))