Вот что я бы сделал с моими любимыми инструментами:
library(data.table)
input_files <- list.files(pattern = "[.]txt$")
years <- stringr::str_extract(input_files, "\\d{4}")
total <- rbindlist(
lapply(input_files, fread),
idcol = "file_id"
)
total[, date := seq(as.Date(paste0(years[file_id],"-01-01")),
as.Date(paste0(years[file_id],"-12-31")), "day"),
by = file_id][
, file_id := NULL]
setcolorder(total, "date")
fwrite(total, "1901-2016.csv")
С моими фиктивными данными содержимое total
выглядит как
date V1 V2 V3
1: 1901-01-01 1901-01-01 1 G
2: 1901-01-02 1901-01-02 2 J
3: 1901-01-03 1901-01-03 3 O
4: 1901-01-04 1901-01-04 4 X
5: 1901-01-05 1901-01-05 5 F
---
42365: 2016-12-27 2016-12-27 362 F
42366: 2016-12-28 2016-12-28 363 P
42367: 2016-12-29 2016-12-29 364 P
42368: 2016-12-30 2016-12-30 365 X
42369: 2016-12-31 2016-12-31 366 N
Воспроизводимые данные
# create dummy data files (in base R)
if (basename(getwd()) != "location") {
dir.create("location")
setwd("location")
}
set.seed(1L)
lapply(1901:2016, function(year){
V1 <- seq(as.Date(paste0(year,"-01-01")), as.Date(paste0(year,"-12-31")), "day")
V2 <- seq_along(V1)
V3 <- sample(LETTERS, length(V1), TRUE)
write.csv(
data.frame(V1, V2, V3, stringsAsFactors = FALSE),
sprintf("E%4i.txt", year),
row.names = FALSE
)
})