如何解决如何从 ncol 文件创建 igraph 列表?
我在 *.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
(我从 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,directed = T) :
number of items to replace is not a multiple of replacement length
2: In my_graphs[g_count] <- read.graph(f,directed = T) :
number of items to replace is not a multiple of replacement length
> my_graphs[1]
[[1]]
[1] 13
那么我在这里做错了什么? list
初始化错误吗?这是无法完成的事情吗?我是否期待不存在的 Pythonic 行为?
解决方法
对于list
,您应该使用
my_graphs[[g_count]] <- read.graph(f,format = "ncol",directed = T)
或
my_graphs[g_count] <- list(read.graph(f,directed = T))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。