我有一个问题与排序数据的基础上的文件的名称。首先,我有一个列表文件包含20 netcdf数据。这是数据列表:
file_all <- list.files(pattern=glob2rx("*.nc"))
> file_all
[1] "bs-cnrmaladin-djf-tas-cru.nc" "bs-cnrmaladin-jja-tas-cru.nc"
[3] "bs-cnrmaladin-mam-tas-cru.nc" "bs-cnrmaladin-son-tas-cru.nc"
[5] "bs-ecearthcclm-djf-tas-cru.nc" "bs-ecearthcclm-jja-tas-cru.nc"
[7] "bs-ecearthcclm-mam-tas-cru.nc" "bs-ecearthcclm-son-tas-cru.nc"
[9] "bs-hadgemhirham-djf-tas-cru.nc" "bs-hadgemhirham-jja-tas-cru.nc"
[11] "bs-hadgemhirham-mam-tas-cru.nc" "bs-hadgemhirham-son-tas-cru.nc"
[13] "bs-mpiracmo-djf-tas-cru.nc" "bs-mpiracmo-jja-tas-cru.nc"
[15] "bs-mpiracmo-mam-tas-cru.nc" "bs-mpiracmo-son-tas-cru.nc"
[17] "bs-noresmremo-djf-tas-cru.nc" "bs-noresmremo-jja-tas-cru.nc"
[19] "bs-noresmremo-mam-tas-cru.nc" "bs-noresmremo-son-tas-cru.nc"
我需要先对它进行排序,因为我需要顺序数据。我想要的顺序是:
djf -> mam -> jja -> son
这是我期望的输出顺序:
[1] "bs-cnrmaladin-djf-tas-cru.nc" "bs-cnrmaladin-mam-tas-cru.nc"
[3] "bs-cnrmaladin-jja-tas-cru.nc" "bs-cnrmaladin-son-tas-cru.nc"
[5] "bs-ecearthcclm-djf-tas-cru.nc" "bs-ecearthcclm-mam-tas-cru.nc"
[7] "bs-ecearthcclm-jja-tas-cru.nc" "bs-ecearthcclm-son-tas-cru.nc"
[9] "bs-hadgemhirham-djf-tas-cru.nc" "bs-hadgemhirham-mam-tas-cru.nc"
[11] "bs-hadgemhirham-jja-tas-cru.nc" "bs-hadgemhirham-son-tas-cru.nc"
[13] "bs-mpiracmo-djf-tas-cru.nc" "bs-mpiracmo-mam-tas-cru.nc"
[15] "bs-mpiracmo-jja-tas-cru.nc" "bs-mpiracmo-son-tas-cru.nc"
[17] "bs-noresmremo-djf-tas-cru.nc" "bs-noresmremo-mam-tas-cru.nc"
[19] "bs-noresmremo-jja-tas-cru.nc" "bs-noresmremo-son-tas-cru.nc"
如果有人知道如何排序与此序列请帮助我。提前感谢
2条答案
按热度按时间smdnsysy1#
您可以提取字符串的这一部分并将其与您的订单匹配,即
以下是一些备选方案:
使用
strsplit
。这可以提高效率,因为它不使用正则表达式。xzlaal3s2#
有点冗长,但您可以使用基于
tidyverse
的解决方案:这个返回
数据
基准测试
由于我很好奇,我做了一个小的基准测试@Sotos答案与这个答案的变体和基于
tidyverse
的解决方案:结果让我相当吃惊
以下是基于
my_list_2
大小的几个基准测试结果:在
~20000
的样本大小下,tidyverse解决方案似乎领先。