【问题标题】:Ordering files numerically based on the middle of the character string in r根据 r 中字符串的中间值对文件进行数字排序
【发布时间】:2021-07-22 05:53:03
【问题描述】:

我在一个目录中有一个 .tif 图像列表,稍后我会循环执行一些光栅数学运算。我希望 .tif 文件按日期排序,这样当循环运行时,我可以根据列表中的顺序知道哪个日期对应于哪个 .tif 文件。我想我需要重命名文件,或者想办法在循环之后对它们进行排序。

例如:

rastlist <- list.files(here::here("data", "s2_cropped"), 
                       pattern = '.tif$', all.files = TRUE, full.names = TRUE)
rastlist
[1] "/data/s2_cropped/S2A2A_20200307_135_36MYD_BOA_10.tif"
[2] "/data/s2_cropped/S2A2A_20200605_135_36MYD_BOA_10.tif" 
[3] "/data/s2_cropped/S2A2A_20200705_135_36MYD_BOA_10.tif" 
[4] "/data/s2_cropped/S2A2A_20200804_135_36MYD_BOA_10.tif" 
[5] "/data/s2_cropped/S2A2A_20200903_135_36MYD_BOA_10.tif" 
[6] "/data/s2_cropped/S2A2A_20201003_135_36MYD_BOA_10.tif" 
[7] "/data/s2_cropped/S2A2A_20201102_135_36MYD_BOA_10.tif" 
[8] "/data/s2_cropped/S2A2A_20201202_135_36MYD_BOA_10.tif" 
[9] "/data/s2_cropped/S2B2A_20200102_135_36MYD_BOA_10.tif" 
[10] "/data/s2_cropped/S2B2A_20200211_135_36MYD_BOA_10.tif" 
[11] "/data/s2_cropped/S2B2A_20200401_135_36MYD_BOA_10.tif" 
[12] "/data/s2_cropped/S2B2A_20200501_135_36MYD_BOA_10.tif" 

我认为它是根据字符串“S2A2A”和“S2B2A”的第一部分按字母顺序排序的,但我希望它按这样的日期排序:

rastlist
[1] "/data/s2_cropped/S2B2A_20200102_135_36MYD_BOA_10.tif" 
[2] "/data/s2_cropped/S2B2A_20200211_135_36MYD_BOA_10.tif"
[3] "/data/s2_cropped/S2A2A_20200307_135_36MYD_BOA_10.tif"
[4] "/data/s2_cropped/S2B2A_20200401_135_36MYD_BOA_10.tif" 
[5] "/data/s2_cropped/S2B2A_20200501_135_36MYD_BOA_10.tif"
[6] "/data/s2_cropped/S2A2A_20200605_135_36MYD_BOA_10.tif" 
[7] "/data/s2_cropped/S2A2A_20200705_135_36MYD_BOA_10.tif" 
[8] "/data/s2_cropped/S2A2A_20200804_135_36MYD_BOA_10.tif" 
[9] "/data/s2_cropped/S2A2A_20200903_135_36MYD_BOA_10.tif" 
[10] "/data/s2_cropped/S2A2A_20201003_135_36MYD_BOA_10.tif" 
[11] "/data/s2_cropped/S2A2A_20201102_135_36MYD_BOA_10.tif" 
[12] "/data/s2_cropped/S2A2A_20201202_135_36MYD_BOA_10.tif" 

然后我循环遍历每个堆栈做一些光栅数学。我不知道这部分代码是否很重要,但它看起来像这样。

ndvi_list <- list() #empty vector to store ndvi values

for  (i in 1:length(rastlist)) {

b <- brick(rastlist[[i]]
ndvi_list[i] <- (b[[4]] - b[[1]]) / (b[[4]] + b[[1]])

}

The result of the loop is 12 rasters in a list

【问题讨论】:

    标签: r list loops rename raster


    【解决方案1】:

    我们可以提取日期子字符串,转换为 Date 类,order 并使用该索引进行排序

    rastlist2 <- rastlist[order(as.Date(sub(".*_(\\d{8})_.*", 
             "\\1", rastlist), '%Y%m%d'))]
    

    -输出

    rastlist2
    #[1] "/data/s2_cropped/S2B2A_20200102_135_36MYD_BOA_10.tif" "/data/s2_cropped/S2B2A_20200211_135_36MYD_BOA_10.tif"
    #[3] "/data/s2_cropped/S2A2A_20200307_135_36MYD_BOA_10.tif" "/data/s2_cropped/S2B2A_20200401_135_36MYD_BOA_10.tif"
    #[5] "/data/s2_cropped/S2B2A_20200501_135_36MYD_BOA_10.tif" "/data/s2_cropped/S2A2A_20200605_135_36MYD_BOA_10.tif"
    #[7] "/data/s2_cropped/S2A2A_20200705_135_36MYD_BOA_10.tif" "/data/s2_cropped/S2A2A_20200804_135_36MYD_BOA_10.tif"
    #[9] "/data/s2_cropped/S2A2A_20200903_135_36MYD_BOA_10.tif" "/data/s2_cropped/S2A2A_20201003_135_36MYD_BOA_10.tif"
    #[11] "/data/s2_cropped/S2A2A_20201102_135_36MYD_BOA_10.tif" "/data/s2_cropped/S2A2A_20201202_135_36MYD_BOA_10.tif"
    

    数据

    rastlist <- c("/data/s2_cropped/S2A2A_20200307_135_36MYD_BOA_10.tif", "/data/s2_cropped/S2A2A_20200605_135_36MYD_BOA_10.tif", 
    "/data/s2_cropped/S2A2A_20200705_135_36MYD_BOA_10.tif", "/data/s2_cropped/S2A2A_20200804_135_36MYD_BOA_10.tif", 
    "/data/s2_cropped/S2A2A_20200903_135_36MYD_BOA_10.tif", "/data/s2_cropped/S2A2A_20201003_135_36MYD_BOA_10.tif", 
    "/data/s2_cropped/S2A2A_20201102_135_36MYD_BOA_10.tif", "/data/s2_cropped/S2A2A_20201202_135_36MYD_BOA_10.tif", 
    "/data/s2_cropped/S2B2A_20200102_135_36MYD_BOA_10.tif", "/data/s2_cropped/S2B2A_20200211_135_36MYD_BOA_10.tif", 
    "/data/s2_cropped/S2B2A_20200401_135_36MYD_BOA_10.tif", "/data/s2_cropped/S2B2A_20200501_135_36MYD_BOA_10.tif"
    )
    

    【讨论】:

    • 这太棒了!你能解释一下为什么 rastlist2 不是最终输出吗?为什么要将所有名称组合回 rastlist?
    • @seak23 抱歉,我没有收到您的问题。我以为你想根据日期重新排序文件
    • 是的,而且效果很好!似乎 rastlist2 已经是重新排序的列表。您只是将 rastlist2 的输出添加回 rastlist 对象吗?
    • @seak23 你可以更新为rastlist &lt;- rastlist[order(as.Date(sub(".*_(\\d{8})_.*", "\\1", rastlist), '%Y%m%d'))] 我在想你需要原始对象用于其他用途
    猜你喜欢
    • 2015-08-13
    • 1970-01-01
    • 1970-01-01
    • 2016-11-07
    • 1970-01-01
    • 2021-09-07
    • 2020-10-26
    • 2021-10-30
    • 2021-11-25
    相关资源
    最近更新 更多