简体   繁体   English

如何根据文件名的某些“日期部分”在R中排序文件

[英]How to order files in R according to certain “date parts” of their names

My question is similar to this question: How to change the order of display of list.files(): for example based on part of the whole name of files 我的问题类似于以下问题: 如何更改list.files()的显示顺序:例如基于文件全名的一部分

but my case is differnt. 但我的情况不同。 I used the solution provided: 我使用了提供的解决方案:

              a=a[order(gsub('.*_(\\d{8})[.].*','\\1',a))]

but did not work for my case because the naming in my case is different from that provided in that question. 但不适用于我的情况,因为我的情况下的命名与该问题中提供的命名不同。

I have several files in a directory. 我的目录中有几个文件。 The naming of the files is complex,for instance : 文件的命名很复杂,例如:

 file.img
 file.img

I want to list the files so that I can work with them, it seemed that R listed them in a certain order. 我想列出这些文件以便可以使用它们,似乎R以一定顺序列出了它们。 R ordered the files alphabetically even if the data is not ordered correctly.for example these two names of files: 即使数据未正确排序,R也会按字母顺序对文件进行排序。例如,这两个文件名:

How can I tell R to change the default display of list.files , and order the files based on _yearmonthday only which represents yearmonthday in all files: 如何告诉R更改list.files的默认显示,并仅基于_yearmonthday 排序文件,该_yearmonthday表示所有文件中的yearmonthday:

to list files in R we use this : 列出R中的文件,我们使用以下命令:

I used: 我用了:

      mixsort 

but did not ordered them as I want 但没有按我的要求订购

I think you just need to change the pattern of the gsub function. 我认为您只需要更改gsub函数的模式即可。 For example: 例如:

xx <- 'SM_OP_20120330T000000_20120330T235959_245_001_7_ssture.img'
gsub('.*_(\\d{8}).*','\\1',xx)
"20120330"

So , the whole solution is : 因此,整个解决方案是:

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

EDIT add an example: 编辑添加示例:

   a <- list('SM_OP_20120330T000000_20120330T235959_245_001_7_ssture.img', ##2012-mars-30
          'SM_RE_20101130T000000_20110427T235959_245_001_7_ssture.img', ##2010-nov-30
          'SM_RE_20100901T000000_20090127T235959_245_001_7_ssture.img', ##2010-sep-01
          'SM_RE_20100904T000000_20090427T235959_245_001_7_ssture.img') ##2010-sep-04


a[order( as.numeric(gsub('.*_(\\d{8}).*','\\1',a)))]

[[1]]
[1] "SM_RE_20100901T000000_20090127T235959_245_001_7_ssture.img"

[[2]]
[1] "SM_RE_20100904T000000_20090427T235959_245_001_7_ssture.img"

[[3]]
[1] "SM_RE_20101130T000000_20110427T235959_245_001_7_ssture.img"

[[4]]
[1] "SM_OP_20120330T000000_20120330T235959_245_001_7_ssture.img"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM