-1

I have several nc files in a directory. The naming of the files is complex,for instance :

 LPRM-AMSR_E_L3_D_SOILM3_V002-20120601T214436Z_20100801.nc

I want to list the files so that I can work with them,it seemed that R listed them ina certain order.

How can I tell R change the default display of list.files , and order them the files based on _yearmonthday only which represents yearmonthday in all files

LPRM-AMSR_E_L3_D_SOILM3_V002-20120601T214436Z _20100801

to list files in R I use this :

a<-list.files("D:\\semon", "*.nc", full.names = TRUE)

I get this :

[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"

I want to get this :

[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"
Barry
  • 739
  • 1
  • 8
  • 29

2 Answers2

4
    a<-list.files("D:\\semon", "*_20100801.nc", full.names = TRUE)

You want this :

 a<-list.files("D:\\semon", "*_[0-9]{8}[.]nc", full.names = TRUE)

or to be match a classic date pattern:

  a<-list.files("D:\\semon", "*_\\d{4}\\d{2}\\d{2}[.]nc", full.names = TRUE)

EDIT

The OP is looking to order the output of list.files

   a<-list.files("D:\\semon", "*.nc", full.names = TRUE)
   a[order(gsub('.*_(\\d{8})[.].*','\\1',a))]

For example:

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"
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • +1 for solving the OP's question after quite some iterations of "question refinement" :) – QkuCeHBH Mar 01 '13 at 16:32
  • 1
    @Barry and rank is different from order(at least in R) . see [this](http://stackoverflow.com/questions/12289224/rank-and-order-in-r). – agstudy Mar 01 '13 at 16:36
2

To list all files whose filenames end on eight numbers plus ".nc":

a <- list.files("/path/to/your/files", "_[0-9]{8}\\.nc$", full.names = TRUE)

To get only valid dates, you would have to adapt the pattern a little, but it wouldn't be a big deal (left as an exercise, if you will).

QkuCeHBH
  • 960
  • 1
  • 9
  • 23
  • 1
    @Barry Then the formulation of your question was wrong. Even in your edited question, it is unclear to me what you want to achieve. You only say "list them like" and then given three example rows. Do you want to sort them by date, by filename, by very-special-foo-random-sort?! – QkuCeHBH Mar 01 '13 at 16:23
  • Sorry to have confused you.I want to sort them by date – Barry Mar 01 '13 at 16:27