0

我有几个类似于“PRISM_ppt_stable_4kmM2_--------_bil.bil”的文件,其中 ------ 是年和月,如 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} 将匹配任何四个数字序列,但这将匹配所有文件。

4

1 回答 1

0

这是我要做的:

# 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]
于 2015-10-23T16:03:15.430 回答