2015-04-23 18 views
1

我在名爲data的文件夾中有成千上萬個不同的文件(具有相同名稱和擴展名.img)。什麼改變只是日期。如果我用這個:如何列出兩個選定日期的數據集?

dir<- list.files ("C:\\Users\\data", "*.img", full.names = TRUE) 

它會列出文件夾中data我的所有文件。

我需要的僅僅是列出從01062005 to 29102008日的文件:

例子如何文件被命名爲:

File_yyyymmdd_data.img  (in which yyyymmdd varies for 10 years) 

任何想法或暗示的讚賞!

+0

我想你需要'paste'和'seq'。先用'as.Date'將'01062005'轉換爲'20102008'到'Date'。對日期序列使用'seq',使用'format'格式將其轉換回原始格式,並用'data.img'粘貼''並使用'lapply'在列表中使用'read.table/read.csv'讀取' – akrun

+0

@sacvf爲什麼不讀取完整的文件集,將名稱的所需部分提取爲單獨的矢量作爲日期,並使用* dated *列將子集值提取出來? – Konrad

+0

@sacvf這就是我所說的'read.table/read.csv'(讀取所有選定日期名稱的文件) – akrun

回答

1

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" 
+0

@sacvf它通過組捕獲將「yyyymmdd」轉換爲「yyyy-mm-dd」正則表達式。 [http://www.regular-expressions.info/rlanguage.html] –

+0

@sacvf在第一個解決方案中,確保文件名中的「yyyymmdd」部分是有效的日期。例如「20050532」會引發這樣的錯誤情況。你可以打印中間結果來找出什麼是錯的。 –

+0

@sacvf我犯了一個錯誤,它應該是'as.POSIXct' in'.to < - as.POSIXct(as.Date(to),tz = tz)'而不是'as.POSIXlt' –

1

繼評論中的討論後,我想建議閱讀完整的文件列表,然後通過常用字符串操作集合創建一個包含日期的正常列,並使用所需日期範圍對新向量進行子集化。例如,

rm(list = ls()) 
mydates <- as.Date(c("2007-06-22", "2004-02-13", "2004-02-14","2004-02-15")) 
subs.dates <- mydates[mydates <= "2004-02-14"] 

子集將包含:

> subs.dates 
[1] "2004-02-13" "2004-02-14" 
相關問題