我对R非常陌生。
我正在尝试使用tidygraph编写以下代码:
V(g)$activated <- F
V(g)[seeds]$activated=T其中g是我的图形,activated是我要添加的属性,seeds是预定义的向量。我成功地使用tidygraph完成了第一部分
g%<>%activate(nodes)%>%
mutate(activated=F)我想知道第二部分该怎么做。
发布于 2021-08-05 06:09:37
我不确定您的seeds变量是什么样子。此解决方案假设seeds是与节点名称相对应的数字向量。下面是重现图形的代码。
library(igraph)
library(tidygraph)
library(tidyverse)
g <- play_erdos_renyi(10, .2)
seeds <- 1:3
V(g)$activated <- FALSE
V(g)[seeds]$activated <- TRUE
g
# # A tbl_graph: 10 nodes and 16 edges
# #
# # A directed simple graph with 1 component
# #
# # Node Data: 10 x 1 (active)
# activated
# <lgl>
# 1 TRUE
# 2 TRUE
# 3 TRUE
# 4 FALSE
# 5 FALSE
# 6 FALSE
# # ... with 4 more rows
# #
# # Edge Data: 16 x 2
# from to
# <int> <int>
# 1 8 1
# 2 4 2
# 3 1 3
# # ... with 13 more rows以下是解决方案。之所以使用row_number(),是因为在这个随机图示例中,节点名称对应于行号。如果您有一个用于节点名的变量,您可以简单地替换row_number()。另外,默认情况下,tidygraph会为节点设置活动数据帧。因此,不需要激活节点。
g <- play_erdos_renyi(10, .2)
g |>
mutate(activated = row_number() %in% seeds)
# # A tbl_graph: 10 nodes and 16 edges
# #
# # A directed simple graph with 1 component
# #
# # Node Data: 10 x 1 (active)
# activated
# <lgl>
# 1 TRUE
# 2 TRUE
# 3 TRUE
# 4 FALSE
# 5 FALSE
# 6 FALSE
# # ... with 4 more rows
# #
# # Edge Data: 16 x 2
# from to
# <int> <int>
# 1 8 1
# 2 4 2
# 3 1 3
# # ... with 13 more rowshttps://stackoverflow.com/questions/68660665
复制相似问题