1

I have a number of weighted graphs stored in *.ncol files. For the sake of the question lets say I have 2 files,

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

I would like to read these into a list of graphs, that is I would like to have a list where my_graphs[1] would give me the graph from ncol_1.ncol

(I've crossed over from Python so my go to data structure is a list, I am open to a better R solution if one exists)

My attempt in 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
}

This is what I get as output,

[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

So what am I doing wrong here? Is the list initialization wrong? is this something that just cant be done? am I expecting Pythonic behaviour where none exists?

DrBwts
  • 3,470
  • 6
  • 38
  • 62

1 Answers1

2

For list, you should use

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

or

my_graphs[g_count] <- list(read.graph(f, format = "ncol", directed = T))
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81