gpt4 book ai didi

r - 将节点集合的邻居名称提取为列表

转载 作者:行者123 更新时间:2023-12-04 02:31:30 24 4
gpt4 key购买 nike

我想提取 igraph 中选定节点的邻居名称作为列表。

这是我目前所拥有的:

library(igraph)
set.seed(100)
g<-erdos.renyi.game(26, 0.4)
V(g)$name<-letters
x<-neighborhood(g, order = 1, V(g)$name %in% c('a', 'd', 'z'))

在上面的示例中,我想提取节点 a、d 和 z 的邻居的名称。这是我得到的输出:

[[1]]
+ 9/26 vertices, named, from 6ba7060:
[1] a c h i l q w x z

[[2]]
+ 11/26 vertices, named, from 6ba7060:
[1] d b c e g h i k l w y

[[3]]
+ 9/26 vertices, named, from 6ba7060:
[1] z a c g h l o u v

我想列出一长串带有代表的名字。输出应如下所示:

[1] "a" "c" "h" "i" "l" "q" "w" "x" "z" "d" "b" "c" "e" "g" "h" "i" "k" "l" "w" "y" "z" "a" "c"
[24] "g" "h" "l" "o" "u" "v

到目前为止,我已经使用库 purrr unlist 和各种版本的 x %>% map(2) %>% flatten(),但无济于事。

我也不反对以 data.frametibble 的形式获取输出,其中一列是名称,另一列是出现次数。

最佳答案

也许你可以试试 names(unlist(x)),例如,

> names(unlist(x))
[1] "a" "c" "h" "i" "l" "q" "w" "x" "z" "d" "b" "c" "e" "g" "h" "i" "k" "l" "w"
[20] "y" "z" "a" "c" "g" "h" "l" "o" "u" "v"

关于r - 将节点集合的邻居名称提取为列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63946752/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com