我在目录中有几个nc文件。文件的命名很复杂,例如:
LPRM-AMSR_E_L3_D_SOILM3_V002-20120601T214436Z_20100801.nc
我想列出文件以便我可以使用它们,似乎R按照一定的顺序列出它们。
如何判断R更改list.files的默认显示,命令基于_yearmonthday只显示所有文件中的yearmonthday的文件
LPRM-AMSR_E_L3_D_SOILM3_V002-20120601T214436Z _20100801
列出R中的文件我用这个:
a<-list.files("D:\\semon", "*.nc", full.names = TRUE)
我明白了:
[1] "D:\\LPRM-AMSR_E_L3_D_SOILM3_V002-20120603T170757Z_20110916.nc"
[2] "D:\\/LPRM-AMSR_E_L3_D_SOILM3_V002-20120603T194524Z_20110917.nc"
[3] "D:\\/LPRM-AMSR_E_L3_D_SOILM3_V002-20120604T122649Z_20110915.nc"
我想得到这个:
[1] "D:\\/LPRM-AMSR_E_L3_D_SOILM3_V002-20120604T122649Z_20110915.nc"
[2] "D:\\/LPRM-AMSR_E_L3_D_SOILM3_V002-20120603T170757Z_20110916.nc"
[3] "D:\\/LPRM-AMSR_E_L3_D_SOILM3_V002-20120603T194524Z_20110917.nc"
答案 0 :(得分:4)
a<-list.files("D:\\semon", "*_20100801.nc", full.names = TRUE)
你想要这个:
a<-list.files("D:\\semon", "*_[0-9]{8}[.]nc", full.names = TRUE)
或匹配经典日期模式:
a<-list.files("D:\\semon", "*_\\d{4}\\d{2}\\d{2}[.]nc", full.names = TRUE)
修改强>
OP希望订购list.files
a<-list.files("D:\\semon", "*.nc", full.names = TRUE)
a[order(gsub('.*_(\\d{8})[.].*','\\1',a))]
例如:
a <- c('D:\\AMSER\\amser2010+2011/LPRM-AMSR_E_L3_D_SOILM3_V002-20120603T170757Z_20110916.nc',
"D:\\AMSER\\amser2010+2011/LPRM-AMSR_E_L3_D_SOILM3_V002-20120603T194524Z_20110917.nc",
"D:\\AMSER\\amser2010+2011/LPRM-AMSR_E_L3_D_SOILM3_V002-20120604T122649Z_20110915.nc")
a[order(gsub('.*_(\\d{8})[.].*','\\1',a))]
[1] "D:\\AMSER\\amser2010+2011/LPRM-AMSR_E_L3_D_SOILM3_V002-20120604T122649Z_20110915.nc"
[2] "D:\\AMSER\\amser2010+2011/LPRM-AMSR_E_L3_D_SOILM3_V002-20120603T170757Z_20110916.nc"
[3] "D:\\AMSER\\amser2010+2011/LPRM-AMSR_E_L3_D_SOILM3_V002-20120603T194524Z_20110917.nc"
答案 1 :(得分:2)
列出文件名以8个数字加“.nc”结尾的所有文件:
a <- list.files("/path/to/your/files", "_[0-9]{8}\\.nc$", full.names = TRUE)
要获得有效的日期,你必须稍微调整一下模式,但这不是什么大不了的事(如果你愿意,可以留下练习。)