我有几个类似于“PRISM_ppt_stable_4kmm_m_-是一年又一个月的年份和月份-比如190112的文件。文件日期从189501到198012不等。在R中,在Windows 7机器上,我想匹配192001到193912年间的所有文件。我很确定我想要grepl(),但我不知道如何引用命令中的序列。我试过了
my.files[grepl('PRISM.*/1920/.bil$',my.files)]和
my.files[grepl('PRISM.*[1][9][2][0].',my.files)]和其他变体,但只需要得到错误信息。我知道0-9{4}将匹配任意四个数字序列,但这将匹配所有文件。
发布于 2015-10-23 16:03:15
我会这样做:
# Reproducible example of file list
library(stringr)
ym <- paste0(1895:1980, str_pad(1:12, 2, pad='0'))
file_list <- paste0("PRISM_ppt_stable_4kmM2_", ym, "_bil.bil")
# Create a list of desired dates and convert to your date format
dates <- seq(as.Date('1920-01-01'), as.Date('1939-12-01'), by='month')
dates <- format(dates, '%Y%m')
# Subset the file list
your_files <- file_list[str_extract(file_list, '[0-9]{6}') %in% dates]https://stackoverflow.com/questions/33306482
复制相似问题