【问题标题】:R - Select files by dates in filenamesR - 按文件名中的日期选择文件
【发布时间】:2018-01-10 16:40:09
【问题描述】:

我在这里已经有一个类似的问题: R - How to choose files by dates in file names?

但我必须做一点改变。

我还有一个文件名列表,类似于:

list = c("AT0ACH10000700100dymax.1-1-1993.31-12-2003",
         "AT0ILL10000700500dymax.1-1-1990.31-12-2011", 
         "AT0PIL10000700500dymax.1-1-1992.31-12-2011",
         "AT0SON10000700100dymax.1-1-1990.31-12-2011",
         "AT0STO10000700100dymax.1-1-1992.31-12-2006",  
         "AT0VOR10000700500dymax.1-1-1981.31-12-2011",
         "AT110020000700100dymax.1-1-1993.31-12-2001",
         "AT2HE190000700100dymax.1-1-1973.31-12-1994", 
         "AT2KA110000700500dymax.1-1-1991.31-12-2010", 
         "AT2KA410000700500dymax.1-1-1991.31-12-2011")

我已经有一个命令来整理记录一定长度的文件(例如本例中的 10 个):

#Listing Files (creates the list above)
files = list.files(pattern="*00007.*dymax", recursive = TRUE)

#Making date readable
split_daymax = strsplit(files, split=".", fixed=TRUE)

from = unlist(lapply(split_daymax, "[[", 2))
to = unlist(lapply(split_daymax, "[[", 3))
from = as.POSIXct(from, format="%d-%m-%Y")
to = as.POSIXct(to, format="%d-%m-%Y")

timelistmax = difftime(to, from, "days")

#Files with more than 10 years of recording
index = timelistmax >= 10*360
filesdaymean = filesdaymean[index]

我现在的问题是我有太多文件,没有计算机可以处理。

现在我只想读入包含 1993 年(或我想要的任何其他特定年份)的文件,并且从那时起有 10 年的录音,所以录音应该至少到 2003 年。

所以不应该包含 1973-1994 的文件,但是 1981-2011 的文件是可以的。

在这种情况下,我不知道如何选择年份。

感谢您的帮助

【问题讨论】:

  • 1973-1994 10多年了
  • 当然,至少10年!它也可以更多。但是 1973-1994 并不好,因为我对 1993-2003 的时间很感兴趣。并且该文件在该期间仅包含 1 年。抱歉,当时不清楚。我想要包含 1993-2003 年的文件。我想排除上面提到的文件,因为它在那个时期只有 1 年,我只想要至少有 10 年的文件。所以 1993-2010 年左右也可以。
  • 我猜你可能需要提到它应该是 10 或 10 的倍数
  • 什么意思?
  • 顺便说一句,在你给的list中,你想选择的只是1、6和10吗?在那种情况下list[!sapply(stringr::str_extract_all(list, "(?<=-)[0-9]{4}"), function(x) diff(as.numeric(x))) %% 10 ]#[1] "AT0ACH10000700100dymax.1-1-1993.31-12-2003" "AT0VOR10000700500dymax.1-1-1981.31-12-2011" [3] "AT2KA410000700500dymax.1-1-1991.31-12-2011"`

标签: r date long-filenames


【解决方案1】:
library(stringr)
library(lubridate)
fileDates <- str_extract_all(files, "[0-9]{1,2}-[0-9]{1,2}-[0-9]{4}")

find_file <- function(x, whichYear, noYears = 10) {
  start <- as.Date(x[[1]], "%d-%m-%Y")
  end <- as.Date(x[[2]], "%d-%m-%Y")
  years <- as.numeric(end-whichYear, units = "days")/365
  years > noYears & (year(start) <= year(whichYear) & 
                       year(end) >= year(whichYear))
}
sapply(fileDates, find_file, whichYear = as.Date("1993-01-01"), noYears = 10)

您有两个条件,您可以首先计算自 1993 年以来的年数,然后使用布尔逻辑来确定 1993 年是否在日期范围内。

【讨论】:

  • 谢谢!这很有效,我得到了我想要的结果!
【解决方案2】:

使用您在上面定义的filestofrom,这应该会得到包含 1993 年至 2003 年之间至少十年数据的文件:

library(lubridate)
df <- data.frame(file_name = files, file_start = from, file_end = to)
df_index <- year(df$file_start) <=1993 & year(df$file_end) >= 2003
files_to_load <- df$file_name[df_index]

如果只需要基本解决方案,请将 POSIXct 转换为 POSIXlt 并提取年份组件:

df <- data.frame(file_name = files, 
                 file_start = as.POSIXlt(from), 
                 file_end = as.POSIXlt(to))

df_index <- (df$file_start$year+1900 <=1993 & 
             df$file_end$year+1900  >= 2003)

files_to_load <- df$file_name[df_index]

【讨论】:

  • 我得到一个错误,说 Error in year(df$file_start) : could not find function "year"
  • 该死,错过了 lubridate 库调用。现在编辑。感谢您的提醒。
猜你喜欢
  • 2018-05-11
  • 1970-01-01
  • 2018-03-28
  • 1970-01-01
  • 2021-02-25
  • 2019-02-28
  • 1970-01-01
  • 2020-08-05
  • 1970-01-01
相关资源
最近更新 更多