按日期列出 R 中的文件

list files in R by dates

我的目录中有一组按日期组织的 netcdf 文件(每个文件都是一天的数据)。我使用

读取了 R 中的所有文件

require(RNetCDF) files= list.files( ,pattern='*.nc',full.names=TRUE)

当我 运行 代码 R 读取 2014 和 2013 时,那么 2010 年的部分内容在末尾..(见下面 R 中的示例输出)

"./MERRA100.prod.assim.tavg1_2d_lnd_Nx.19820223.SUB.nc"
"./MERRA100.prod.assim.tavg1_2d_lnd_Nx.19820224.SUB.nc"
"./MERRA100.prod.assim.tavg1_2d_lnd_Nx.19820225.SUB.nc"

"./MERRA301.prod.assim.tavg1_2d_lnd_Nx.20130829.SUB.nc"
"./MERRA301.prod.assim.tavg1_2d_lnd_Nx.20130830.SUB.nc"
"./MERRA301.prod.assim.tavg1_2d_lnd_Nx.20130831.SUB.nc"

"./MERRA301.prod.assim.tavg1_2d_lnd_Nx.20100626.SUB.nc"
"./MERRA301.prod.assim.tavg1_2d_lnd_Nx.20100827.SUB.nc"
"./MERRA301.prod.assim.tavg1_2d_lnd_Nx.20100828.SUB.nc"

我正在尝试使用循环为这些文件生成每日时间序列..所以当我应用我的其余代码时..从 2010 年 6 月到 2010 年 8 月的数据将结束每日时间序列。我宁愿怀疑这与文件的列出方式有关 R

有没有什么方法可以在 R 中列出文件并确保它是有组织的日期?

这是您未排序的文件

paths <- c("./MERRA100.prod.assim.tavg1_2d_lnd_Nx.19820223.SUB.nc",
           "./MERRA100.prod.assim.tavg1_2d_lnd_Nx.19820224.SUB.nc",
           "./MERRA100.prod.assim.tavg1_2d_lnd_Nx.19820225.SUB.nc",
           "./MERRA301.prod.assim.tavg1_2d_lnd_Nx.20130829.SUB.nc",
           "./MERRA301.prod.assim.tavg1_2d_lnd_Nx.20130830.SUB.nc",
           "./MERRA301.prod.assim.tavg1_2d_lnd_Nx.20130831.SUB.nc",
           "./MERRA301.prod.assim.tavg1_2d_lnd_Nx.20100626.SUB.nc",
           "./MERRA301.prod.assim.tavg1_2d_lnd_Nx.20100827.SUB.nc",
           "./MERRA301.prod.assim.tavg1_2d_lnd_Nx.20100828.SUB.nc")

我正在使用正则表达式提取日期 YYYYMMDD 中的 8 位数字,您应该能够按数字串排序,但您也可以将它们转换为日期

## matches ...Nx.<number of digits = 8>... and captures the stuff in <>
## and saves this match to the first capture group, \1
pattern <- '.*Nx\.(\d{8}).*'

gsub(pattern, '\1', paths)
# [1] "19820223" "19820224" "19820225" "20130829" "20130830" "20130831"
# [7] "20100626" "20100827" "20100828"

sort(gsub(pattern, '\1', paths))
# [1] "19820223" "19820224" "19820225" "20100626" "20100827" "20100828"
# [7] "20130829" "20130830" "20130831"

## not necessary to convert that into dates but you can
as.Date(sort(gsub(pattern, '\1', paths)), '%Y%m%d')
# [1] "1982-02-23" "1982-02-24" "1982-02-25" "2010-06-26" "2010-08-27"
# [6] "2010-08-28" "2013-08-29" "2013-08-30" "2013-08-31"

并对原始路径进行排序

## so you can use the above to order the paths
paths[order(gsub(pattern, '\1', paths))]
# [1] "./MERRA100.prod.assim.tavg1_2d_lnd_Nx.19820223.SUB.nc"
# [2] "./MERRA100.prod.assim.tavg1_2d_lnd_Nx.19820224.SUB.nc"
# [3] "./MERRA100.prod.assim.tavg1_2d_lnd_Nx.19820225.SUB.nc"
# [4] "./MERRA301.prod.assim.tavg1_2d_lnd_Nx.20100626.SUB.nc"
# [5] "./MERRA301.prod.assim.tavg1_2d_lnd_Nx.20100827.SUB.nc"
# [6] "./MERRA301.prod.assim.tavg1_2d_lnd_Nx.20100828.SUB.nc"
# [7] "./MERRA301.prod.assim.tavg1_2d_lnd_Nx.20130829.SUB.nc"
# [8] "./MERRA301.prod.assim.tavg1_2d_lnd_Nx.20130830.SUB.nc"
# [9] "./MERRA301.prod.assim.tavg1_2d_lnd_Nx.20130831.SUB.nc"