Вот подход data.table.
baseDir <- "~/ex/ex1/data_T/"
setwd(baseDir)
library(data.table)
files <- list.files(recursive=TRUE, pattern = "*.txt")
DT <- rbindlist(lapply(setNames(as.list(files), files), fread), idcol = "file")
DT[, `:=` (MinMax = factor(grepl("max", file), labels = c("min", "max")),
p = as.numeric(gsub("(.*/p)([0-9]+)(.txt)", "\\2", file)))]
DT[, idx := 1:.N, by=list(p, MinMax)]
setkeyv(DT, c("p", "idx"))
DT <- dcast(DT[, -"file"], p + idx ~ MinMax, value.var="V1", fun.aggregate = NULL)
DT
#> p idx min max
#> 1: 1 1 19800101.0 19800101.0
#> 2: 1 2 23.2 34.1
#> 3: 1 3 24.0 32.6
#> 4: 1 4 23.3 34.1
#> 5: 1 5 23.8 34.6
#> ---
#> 216252: 16 13512 24.0 36.5
#> 216253: 16 13513 22.6 34.5
#> 216254: 16 13514 22.1 35.1
#> 216255: 16 13515 22.1 35.9
#> 216256: 16 13516 22.7 36.4
head(DT[, .(min, max)])
#> min max
#> 1: 19800101.0 19800101.0
#> 2: 23.2 34.1
#> 3: 24.0 32.6
#> 4: 23.3 34.1
#> 5: 23.8 34.6
#> 6: 23.2 33.9
## now you can save csv files
fwrite(DT[, .(min, max)], file = "MinMax.csv", col.names = FALSE)
## or write them out into separate files:
outfiles <- split(DT, DT$p)
lapply(seq_along(outfiles), function(x) fwrite(outfiles[[x]][, .(min, max)], file = paste0("MinMax_p", x, ".csv"), col.names = FALSE ))
Создан в 2020-03-26 пакетом Представить (v0.3.0)