如何从 ncol 文件创建 igraph 列表?

How to create a list of igraphs from ncol files?

我有一些加权图存储在 *.ncol 文件中。为了这个问题,假设我有 2 个文件,

ncol_1.ncol

21 53 1.0
5 52 1.0
32 52 1.0
119 119 0.5
119 85 0.5
87 0 1.0
36 116 1.0
85 87 1.0
116 5 1.0
4 52 1.0
115 4 1.0
53 115 1.0
52 36 0.3333333333333333
52 21 0.3333333333333333
52 119 0.3333333333333333

ncol_2.ncol

21 115 1.0
119 85 1.0
87 0 1.0
85 87 1.0
4 48 0.3333333333333333
4 20 0.6666666666666666
115 4 1.0
55 119 1.0
48 4 1.0
20 4 0.25
20 20 0.25
20 21 0.25
20 55 0.25
0 20 1.0

我想将这些读入图表列表,也就是说,我想要一个列表,其中 my_graphs[1] 会给我来自 ncol_1.ncol[= 的图表36=]

(我已经从 Python 跨越,所以我的数据结构是一个列表,如果存在更好的 R 解决方案,我愿意接受)

我在 R 中的尝试,

library(igraph)

f_list <- list.files(pattern = "\.ncol$")
set.seed(123)
my_graphs <- list(length(f_list)

g_count = 1
for (f in f_list){
  print(f)
  my_graphs[g_count] <- read.graph(f, format = "ncol", directed = T)
  g_count = g_count + 1
}

这是我得到的输出,

[1] "r_test_1_0_100.ncol"
[1] "r_test_1_0_125.ncol"
Warning messages:
1: In my_graphs[g_count] <- read.graph(f, format = "ncol", directed = T) :
  number of items to replace is not a multiple of replacement length
2: In my_graphs[g_count] <- read.graph(f, format = "ncol", directed = T) :
  number of items to replace is not a multiple of replacement length
> my_graphs[1]
[[1]]
[1] 13

那么我做错了什么? list 初始化错误吗?这是做不到的事情吗?我是否期待 Pythonic 行为 none 存在?

对于list,你应该使用

my_graphs[[g_count]] <- read.graph(f, format = "ncol", directed = T)

my_graphs[g_count] <- list(read.graph(f, format = "ncol", directed = T))