1)如果你有 「過時的名稱」,格式爲在您的文章文件: 「File_yyyymmdd_data.img」
list.files.by.datedNames <- function(
from, # time starts
to, # time ends
tz="GMT", # time zone
myfiles # a vector of all files
){
mydates <- gsub("File_([0-9]{4})([0-9]{2})([0-9]{2})_data.img","\\1-\\2-\\3",myfiles)
.from <- as.POSIXct(from,tz=tz)
.to <- as.POSIXct(to,tz=tz)
.whether <- vapply(mydates,function(e){
.e <- as.POSIXct(e,tz=tz);
as.numeric(.e)>=as.numeric(.from) & as.numeric(.e)<=as.numeric(.to)
},logical(1),USE.NAMES=FALSE)
ret <- myfiles[.whether]
return(ret)
}
## Call it in your case
list.files.by.datedNames(
"2005-06-01","2008-10-29",
myfiles=list.files("C:\\Users\\data", "*.img"))
## ## A tested example to touch and check files in current working directory
## > system("touch File_20050101_data.img")
## > system("touch File_20060101_data.img")
## > system("touch File_20080101_data.img")
## > system("touch File_20090101_data.img")
## >
## > list.files.by.datedNames(
## + "2005-06-01","2008-10-29",
## + myfiles=list.files(pattern="*.img"))
## [1] "File_20060101_data.img" "File_20080101_data.img"
2),或者你選擇通過修改/創建/訪問時文件:
list.files2 <- function(
from, # time starts
to, # time ends
tz="GMT", # time zone
which.time=c("mtime","ctime","atime"), # modification/creation/access time?
... # additional arguments passed to `list.files` function
){
which.time <- match.arg(which.time)
.files <- list.files(...)
.from <- as.POSIXct(from,tz=tz)
.to <- as.POSIXct(to,tz=tz)
.whether <- vapply(.files,function(e){
tmp=file.info(e)[[which.time]];
as.numeric(tmp)>=as.numeric(.from) & as.numeric(tmp)<=as.numeric(.to)
},logical(1),USE.NAMES=FALSE)
ret <- .files[.whether]
return(ret)
}
## Call it in your case
list.files2("2005-06-01","2008-10-29",path="C:\\Users\\data",pattern="*.img")
## ## Continue with the above example [in 1)] to check files that were modified today;
## ## set `tz` according to your time zone.
## > list.files2("2015-04-23","2015-04-24",tz="EDT",pattern="*.img")
## [1] "File_20050101_data.img" "File_20060101_data.img" "File_20080101_data.img"
## [4] "File_20090101_data.img"
我想你需要'paste'和'seq'。先用'as.Date'將'01062005'轉換爲'20102008'到'Date'。對日期序列使用'seq',使用'format'格式將其轉換回原始格式,並用'data.img'粘貼''並使用'lapply'在列表中使用'read.table/read.csv'讀取' – akrun
@sacvf爲什麼不讀取完整的文件集,將名稱的所需部分提取爲單獨的矢量作爲日期,並使用* dated *列將子集值提取出來? – Konrad
@sacvf這就是我所說的'read.table/read.csv'(讀取所有選定日期名稱的文件) – akrun