如何按正确的日期顺序对list.files()进行排序?

huangapple go评论50阅读模式
英文:

how to sort list.files() in correct date order?

问题

使用普通的 list.files() 在工作目录中返回文件列表,但数字顺序混乱。

f <- list.files(pattern="*.nc")
f
# [1] "te1971-1.nc"  "te1971-10.nc" "te1971-11.nc" "te1971-12.nc"
# [5] "te1971-2.nc"  "te1971-3.nc"  "te1971-4.nc"  "te1971-5.nc" 
# [9] "te1971-6.nc"  "te1971-7.nc"  "te1971-8.nc"  "te1971-9.nc"

其中 &quot;-&quot; 后面的数字表示月份。

我尝试使用以下方法进行排序:

myFiles <- paste("te", i, "-", c(1:12), ".nc", sep = "")
mixedsort(myFiles)

它返回排序后的文件,但是是倒序的:

[1] "te1971-12.nc" "te1971-11.nc" "tev1971-10.nc" "te1971-9.nc" 
[5] "te1971-8.nc"  "te1971-7.nc"  "te1971-6.nc"  "te1971-5.nc" 
[9] "te1971-4.nc"  "te1971-3.nc"  "te1971-2.nc"  "te1971-1.nc"

如何修复这个问题?

英文:

Using normal list.files() in the working directory return the file list but the numeric order is messed up.

f &lt;- list.files(pattern=&quot;*.nc&quot;)
f
# [1] &quot;te1971-1.nc&quot;  &quot;te1971-10.nc&quot; &quot;te1971-11.nc&quot; &quot;te1971-12.nc&quot;
# [5] &quot;te1971-2.nc&quot;  &quot;te1971-3.nc&quot;  &quot;te1971-4.nc&quot;  &quot;te1971-5.nc&quot; 
# [9] &quot;te1971-6.nc&quot;  &quot;te1971-7.nc&quot;  &quot;te1971-8.nc&quot;  &quot;te1971-9.nc&quot;

where the number after &quot;-&quot; describes the month number.

I used the following to try to sort it

myFiles &lt;- paste(&quot;te&quot;, i, &quot;-&quot;, c(1:12), &quot;.nc&quot;, sep = &quot;&quot;)
mixedsort(myFiles)

it returns ordered files but in reverse:

[1] &quot;te1971-12.nc&quot; &quot;te1971-11.nc&quot; &quot;tev1971-10.nc&quot; &quot;te1971-9.nc&quot; 
[5] &quot;te1971-8.nc&quot;  &quot;te1971-7.nc&quot;  &quot;te1971-6.nc&quot;  &quot;te1971-5.nc&quot; 
[9] &quot;te1971-4.nc&quot;  &quot;te1971-3.nc&quot;  &quot;te1971-2.nc&quot;  &quot;te1971-1.nc&quot; 

How do I fix this?

答案1

得分: 0

问题是值被按字母顺序排序。

您可以使用 gsub 将年份和月份替换为组 (.),并将 &quot;-1&quot; 添加为月份的第一天以获得收益,然后使用 as.Date 强制转换并按照它进行排序。

x[order(as.Date(gsub(&#39;.*(\\d{4})-(\\d{,2}).*&#39;, &#39;\-\-1&#39;, x)))]
# [1] &quot;te1971-1.nc&quot;  &quot;te1971-2.nc&quot;  &quot;te1971-3.nc&quot;  &quot;te1971-4.nc&quot;  &quot;te1971-5.nc&quot; 
# [6] &quot;te1971-6.nc&quot;  &quot;te1971-7.nc&quot;  &quot;te1971-8.nc&quot;  &quot;te1971-9.nc&quot;  &quot;te1971-10.nc&quot;
# [11] &quot;te1971-11.nc&quot; &quot;te1971-12.nc&quot;

数据:

x &lt;- c(&quot;te1971-1.nc&quot;, &quot;te1971-10.nc&quot;, &quot;te1971-11.nc&quot;, &quot;te1971-12.nc&quot?, 
       &quot;te1971-2.nc&quot;, &quot;te1971-3.nc&quot;, &quot;te1971-4.nc&quot;, &quot;te1971-5.nc&quot;, &quot;te1971-6.nc&quot?, 
       &quot;te1971-7.nc&quot;, &quot;te1971-8.nc&quot;, &quot;te1971-9.nc&quot;)
英文:

The issue is that the values get alphabetically sorted.

You could gsub out years and months as groups (.) and add &quot;-1&quot; as first day of the month to the yield, coerce it as.Date and order by that.

x[order(as.Date(gsub(&#39;.*(\\d{4})-(\\d{,2}).*&#39;, &#39;\-\-1&#39;, x)))]
# [1] &quot;te1971-1.nc&quot;  &quot;te1971-2.nc&quot;  &quot;te1971-3.nc&quot;  &quot;te1971-4.nc&quot;  &quot;te1971-5.nc&quot; 
# [6] &quot;te1971-6.nc&quot;  &quot;te1971-7.nc&quot;  &quot;te1971-8.nc&quot;  &quot;te1971-9.nc&quot;  &quot;te1971-10.nc&quot;
# [11] &quot;te1971-11.nc&quot; &quot;te1971-12.nc&quot;

Data:

x &lt;- c(&quot;te1971-1.nc&quot;, &quot;te1971-10.nc&quot;, &quot;te1971-11.nc&quot;, &quot;te1971-12.nc&quot;, 
       &quot;te1971-2.nc&quot;, &quot;te1971-3.nc&quot;, &quot;te1971-4.nc&quot;, &quot;te1971-5.nc&quot;, &quot;te1971-6.nc&quot;, 
       &quot;te1971-7.nc&quot;, &quot;te1971-8.nc&quot;, &quot;te1971-9.nc&quot;)

huangapple
  • 本文由 发表于 2023年2月14日 08:17:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75442366.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定