根据r中字符串的中间数字排序文件

Ordering files numerically based on the middle of the character string in r

我在一个目录中有一个 .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

我们可以提取日期子字符串,转换为 Date class、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"
)