R根据子字符串对字符串排序 [英] R Sort strings according to substring

查看:701
本文介绍了R根据子字符串对字符串排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组文件名,例如:

I have a set of file names like:

filelist <- c("filea-10.txt", "fileb-2.txt", "filec-1.txt", "filed-5.txt", "filef-4.txt")

,我想根据-"后的数字过滤它们.

and I would like to filter them according to the number after "-".

例如,在python中,我可以使用排序函数的key参数:

In python, for instance, I can use the keyparameter of the sorting function:

filelist <- ["filea-10.txt", "fileb-2.txt", "filec-1.txt", "filed-5.txt", "filef-4.txt"]
sorted(filelist, key=lambda(x): int(x.split("-")[1].split(".")[0]))

> ["filec-1.txt", "fileb-2.txt", "filef-4.txt", "filed-5.txt", "filea-10.txt"]

在R中,我正在玩strsplitlapply,到目前为止还没有运气.

In R, I am playing with strsplit and lapply with no luck so far.

在R中使用哪种方法?

修改: 文件名可以是很多东西,并且可以包含更多数字.唯一固定的模式是我要排序的数字在-"之后.另一个(真实的)示例:

Edit: File names can be many things and may include more numbers. The only fixed pattern is that the number I want to sort by is after the "-". Another (real) example:

c <- ("boards10017-51.mp4",  "boards10065-66.mp4",  "boards10071-81.mp4",
      "boards10185-91.mp4", "boards10212-63.mp4",  "boards1025-51.mp4",   
      "boards1026-71.mp4",   "boards10309-89.mp4", "boards10310-68.mp4",  
      "boards10384-50.mp4",  "boards10398-77.mp4",  "boards10419-119.mp4", 
      "boards10421-85.mp4",  "boards10444-87.mp4",  "boards10451-60.mp4",  
      "boards10461-81.mp4",  "boards10463-52.mp4",  "boards10538-83.mp4",  
      "boards10575-62.mp4",  "boards10577-249.mp4")"

推荐答案

我不确定文件名列表的实际复杂性,但是类似以下内容可能就足够了:

I'm not sure of the actual complexity of your list of file names, but something like the following might be sufficient:

filelist[order(as.numeric(gsub("[^0-9]+", "", filelist)))]
# [1] "filec-1.txt"  "fileb-2.txt"  "filef-4.txt"  "filed-5.txt"  "filea-10.txt"


考虑您的编辑,您可能需要将gsub更改为以下内容:


Considering your edit, you may want to change the gsub to something like:

gsub(".*-|\\..*", "", filelist)

同样,在没有更多文本情况的情况下,很难说这是否满足您的需求.

Again, without a few more text cases, it's hard to say whether this is sufficient for your needs.

示例:

 x <- c("boards10017-51.mp4", "boards10065-66.mp4", "boards10071-81.mp4", 
     "boards10185-91.mp4", "boards10212-63.mp4", "boards1025-51.mp4",     
     "boards1026-71.mp4", "boards10309-89.mp4", "boards10310-68.mp4",     
     "boards10384-50.mp4", "boards10398-77.mp4", "boards10419-119.mp4",   
     "boards10421-85.mp4", "boards10444-87.mp4", "boards10451-60.mp4",    
     "boards10461-81.mp4", "boards10463-52.mp4", "boards10538-83.mp4",    
     "boards10575-62.mp4", "boards10577-249.mp4")  

x[order(as.numeric(gsub(".*-|\\..*", "", x)))]
##  [1] "boards10384-50.mp4"  "boards10017-51.mp4"  "boards1025-51.mp4"  
##  [4] "boards10463-52.mp4"  "boards10451-60.mp4"  "boards10575-62.mp4" 
##  [7] "boards10212-63.mp4"  "boards10065-66.mp4"  "boards10310-68.mp4" 
## [10] "boards1026-71.mp4"   "boards10398-77.mp4"  "boards10071-81.mp4" 
## [13] "boards10461-81.mp4"  "boards10538-83.mp4"  "boards10421-85.mp4" 
## [16] "boards10444-87.mp4"  "boards10309-89.mp4"  "boards10185-91.mp4" 
## [19] "boards10419-119.mp4" "boards10577-249.mp4" 

这篇关于R根据子字符串对字符串排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆