【问题标题】:How to include a year value in date sequence in R?如何在 R 中的日期序列中包含年份值?
【发布时间】:2018-12-08 09:03:42
【问题描述】:

我的文件名称带有年份。我列出了所有文件并将年份值取为变量名。我想在输出文件中创建一个包含所有 365 天的列。但是如何在日期序列中给出具有年份值的变量名称?

文件名的格式为“E1901.txt”、“E1902.txt”、.... 这是脚本


setwd("location")
input_files = list.files(,pattern="[.]txt$")
total = data.frame()
for(i in 1:length(input_files)){
    rf = read.csv(input_files[i])
    year = regmatches(rf,regexec("E(.+).txt",rf))
    year=sapply(year,"[",2)
    print(year)
    filenm = sub("txt","csv",rf)
    date = seq(as.Date(paste(year,"/1/1")), as.Date(paste(year,"/12/31")), "day")
    rf$date = date
    rf= rf[,c(220,1:219)]
}
cat("\n Finished processing data of ",filenm)
total = do.call("rbind",rf)
write.csv(total, file="1901-2016.csv", row.names=FALSE, col.names=FALSE,sep =",")

问候,

【问题讨论】:

标签: r loops date filenames


【解决方案1】:

这就是我会用我最喜欢的工具做的事情:

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
  )
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-18
    • 2021-12-31
    • 2023-02-22
    • 1970-01-01
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多