-1

I have a directory with plethora of files; each file has the same structure:

Nodes: 6606
Edges: 382386
Average degree: 115.76930063578565
Average clustering: 0.11213868344294504
Modularity: 0.6021229084216876
Giant component: 6598

Using the list.files() function I read the content of the directory:

files <- list.files(path = "test", pattern = "netstat*", full.names = TRUE)

Then I use lapply() function to read files into the list of data frames:

data1 <- lapply(files, read.table, sep = ":", row.names = 1)

Finally I convert list to data frame and rename row names:

data2 <- t(do.call(data.frame, data1))
rownames(data2) <- 1:nrow(data)

The final data looks like:

> head(data2)
  Nodes  Edges Average degree Average clustering Modularity Giant component
1  6606 382386     115.769301         0.11213868  0.6021229            6598
2  5157  20292       7.869692         0.07020251  0.8195294            5125
3  5177  20148       7.783658         0.07640135  0.9030172            5102
4  5689  29559      10.391633         0.08480404  0.7104452            5626
5  5985  32086      10.722139         0.06803845  0.7189815            5938
6  5829  26449       9.074970         0.05963236  0.7061715            5770

My question: Is there more elegant way to do that? Especially last command - where I manually rename rows - is somehow not in line with elegant R programming.

Andrej
  • 3,719
  • 11
  • 44
  • 73

1 Answers1

1

We could read the files using fread and convert the list of data.tables to a single data.table with rbindlist

library(data.table)
rbindlist(lapply(files, fread))
akrun
  • 874,273
  • 37
  • 540
  • 662