【问题标题】:Sort file names that have increased numeric characters对数字字符增加的文件名进行排序
【发布时间】:2014-08-11 20:53:26
【问题描述】:

我正在使用此代码通过 mtime 读取我的文件。但是,读取文件的速度非常快,以至于 mtime 不起作用。

###### checks all files in directory ########
readinfiles<-function(){
details<- file.info(list.files("filename", all.files=F, full.name=T));
details<- details[with(details, order(as.POSIXct(mtime))),]
file<- rownames(details)
}
all_files<- readinfiles();
list_all_files<- as.list(all_files);
list_all_files;

有没有办法按字符拼写数字对文件进行排序? 这就是我想要的。

list_all_files;
THREE20142305//tablesCORRECTED///onea
THREE20142305//tablesCORRECTED///twoa
THREE20142305//tablesCORRECTED///threea
THREE20142305//tablesCORRECTED///foura

我得到了什么:

list_all_files;
THREE20142305//tablesCORRECTED///foura
THREE20142305//tablesCORRECTED///onea
THREE20142305//tablesCORRECTED///threea
THREE20142305//tablesCORRECTED///twoa    

【问题讨论】:

  • 还想指出,我的代码现在只要在我的实验中使用它就可以工作。我需要适应阅读 onea、twoa 等的工作......
  • 拼出的数字是否超过nine?或者它们是否至少有一个可管理的上限,您可以在其中枚举它们?
  • 呃...sort(list_all_files)?我可能很天真,但是……
  • 你的号码名有多高?只到“四”吗?
  • 他们拼写到 100

标签: r file sorting


【解决方案1】:

可以使用english 包创建一些辅助函数来解决这个问题

辅助函数

FileSort <- function(x){
  require(english, quietly = TRUE) # Loading the `english` package
  Nums <- as.character(english(seq_len(length(x)))) # Creating a vector of integers written in words (with the same length of the file list)
  Nums <- gsub("\\s", "", Nums) # Remove spaces so, for example, "twenty two" will become "twentytwo"
  temp <- gsub(".*//", "", x) # Retrieving the number out of the file name
  temp <- substr(temp, 1, nchar(temp) - 1) # Removing the `a` at the end
  x <- Map(cbind, x, match(temp, Nums)) # Adding the Numbers column to the file list
  x <- do.call(rbind, x) # Collapsing
  x <- as.list(x[order(as.numeric(x[, 2]))]) # Sorting
  x
}

您的数据

ist_all_files <- list("THREE20142305//tablesCORRECTED///foura",
                       "THREE20142305//tablesCORRECTED///onea",
                       "THREE20142305//tablesCORRECTED///threea",
                       "THREE20142305//tablesCORRECTED///twoa")

实施

list_all_files <- FileSort(list_all_files)
list_all_files
# [[1]]
# [1] "THREE20142305//tablesCORRECTED///onea"
# 
# [[2]]
# [1] "THREE20142305//tablesCORRECTED///twoa"
# 
# [[3]]
# [1] "THREE20142305//tablesCORRECTED///threea"
# 
# [[4]]
# [1] "THREE20142305//tablesCORRECTED///foura"

【讨论】:

  • @David,你是怎么找到英文包的?这太棒了!
  • @ 这是天真的大卫,但是安装这个英文包的语法是什么? install.packages("英文"); ?
  • 是的。你用什么作为你的GUI?在其中大多数(如 RStudio)中,您只需按一下按钮即可完成
  • R 2.10.1 GUI 1.31 Leopard build 32-bit (5537) ,是我正在做的。
  • @david,我删除了日期部分并且效果很好。日期并不重要。对于那个很抱歉。这个答案太棒了!
【解决方案2】:

我可以想出一些方法来解决这个问题,但老实说,它们看起来都很不方便。重命名磁盘上的文件是否可行?如果是我,我会这样做。将它们命名为

THREE20142305//tablesCORRECTED///01_onea
THREE20142305//tablesCORRECTED///02_twoa
THREE20142305//tablesCORRECTED///03_threea
THREE20142305//tablesCORRECTED///04_foura

然后按名称排序,而不是 mtime

您可以通过 GUI 手动重命名文件,所用时间比编写 R 解决方案所需的时间更短,即使有 100 个文件也是如此。如果这些文件被定期覆盖,那么编写一个脚本来一个一个地重命名它们(大量使用复制/粘贴)可能比编写 R 解决方法更快。该脚本将有 100 行几乎相同的行并且编写起来很乏味,但仍然比让 R 理解英语数字单词更容易。

很抱歉带来坏消息。

============= 预处理脚本示例 =================

file.rename("onea","01")
file.rename("twoa","02")
file.rename("threea","03")
file.rename("foura","04")
file.rename("fivea","05")
file.rename("sixa","06")
file.rename("sevena","07")
file.rename("eighta","08")
file.rename("ninea","09")
file.rename("tena","10")
file.rename("elevena","11")
file.rename("twelvea","12")
file.rename("thirteena","13")
file.rename("fourteena","14")
file.rename("fifteena","15")
file.rename("sixteena","16")
file.rename("seventeena","17")
file.rename("eighteena","18")
file.rename("nineteena","19")
file.rename("twentya","20")
file.rename("twentyonea","21")
file.rename("twentytwoa","22")
file.rename("twentythreea","23")
file.rename("twentyfoura","24")
file.rename("twentyfivea","25")
file.rename("twentysixa","26")
file.rename("twentysevena","27")
file.rename("twentyeighta","28")
file.rename("twentynine","29")
file.rename("twentya","20")

复制粘贴并再调整 10 分钟左右(取决于您的文本编辑器搜索/替换技能),将其放在程序的顶部,如果您没有从某人那里得到更好的解决方案,您就有一个可行的解决方案否则。

【讨论】:

  • 是的,每天这些文件都会被覆盖。这是个坏消息。我可以手动浏览并重命名它们。只是希望快速修复。谢谢
猜你喜欢
  • 2012-04-01
  • 2016-07-02
  • 2019-04-12
  • 2016-05-03
  • 1970-01-01
  • 2010-10-01
  • 1970-01-01
  • 2021-07-02
相关资源
最近更新 更多