Home > Mobile >  Selecting the first few files among many in a vector
Selecting the first few files among many in a vector

Time:11-29

I have a vector named rastlist that lists all of the files specified using this code:

rastlist <- list.files(path = "raw_data/MODIS_TERRA/NDVI_FinalFitted", pattern=c('2013','.TIF$'), all.files=TRUE, 
                       full.names=TRUE)

Here is what that object contains:

The numbers after MOD09Q1_2013_ (e.g., 001, 002, 003....) refer to the days in the year. The rest of the name is the same other than the days in the year.

rastlist
  [1] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_001_NDVI_FinalFitted.tif"
  [2] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_002_NDVI_FinalFitted.tif"
  [3] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_003_NDVI_FinalFitted.tif"
  [4] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_004_NDVI_FinalFitted.tif"
  [5] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_005_NDVI_FinalFitted.tif"
  [6] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_006_NDVI_FinalFitted.tif"
  [7] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_007_NDVI_FinalFitted.tif"
  [8] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_008_NDVI_FinalFitted.tif"
  [9] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_009_NDVI_FinalFitted.tif"
 [10] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_010_NDVI_FinalFitted.tif"
 [11] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_011_NDVI_FinalFitted.tif"
 [12] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_012_NDVI_FinalFitted.tif"
 [13] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_013_NDVI_FinalFitted.tif"
 [14] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_014_NDVI_FinalFitted.tif"
 [15] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_015_NDVI_FinalFitted.tif"
 [16] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_016_NDVI_FinalFitted.tif"
 [17] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_017_NDVI_FinalFitted.tif"
 [18] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_018_NDVI_FinalFitted.tif"
 [19] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_019_NDVI_FinalFitted.tif"
 [20] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_020_NDVI_FinalFitted.tif"
 [21] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_021_NDVI_FinalFitted.tif"
 [22] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_022_NDVI_FinalFitted.tif"
 [23] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_023_NDVI_FinalFitted.tif"
 [24] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_024_NDVI_FinalFitted.tif"
 [25] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_025_NDVI_FinalFitted.tif"
 [26] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_026_NDVI_FinalFitted.tif"
 [27] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_027_NDVI_FinalFitted.tif"
 [28] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_028_NDVI_FinalFitted.tif"
 [29] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_029_NDVI_FinalFitted.tif"
 [30] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_030_NDVI_FinalFitted.tif"
 [31] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_031_NDVI_FinalFitted.tif"
 [32] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_032_NDVI_FinalFitted.tif"
 [33] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_033_NDVI_FinalFitted.tif"
 [34] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_034_NDVI_FinalFitted.tif"
 [35] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_035_NDVI_FinalFitted.tif"
 [36] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_036_NDVI_FinalFitted.tif"
 [37] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_037_NDVI_FinalFitted.tif"
 [38] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_038_NDVI_FinalFitted.tif"
 [39] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_039_NDVI_FinalFitted.tif"
 [40] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_040_NDVI_FinalFitted.tif"
 [41] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_041_NDVI_FinalFitted.tif"
 [42] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_042_NDVI_FinalFitted.tif"
 [43] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_043_NDVI_FinalFitted.tif"
 [44] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_044_NDVI_FinalFitted.tif"
 [45] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_045_NDVI_FinalFitted.tif"
 [46] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_046_NDVI_FinalFitted.tif"
 [47] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_047_NDVI_FinalFitted.tif"
 [48] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_048_NDVI_FinalFitted.tif"
 [49] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_049_NDVI_FinalFitted.tif"
 [50] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_050_NDVI_FinalFitted.tif"
 [51] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_051_NDVI_FinalFitted.tif"
 [52] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_052_NDVI_FinalFitted.tif"
 [53] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_053_NDVI_FinalFitted.tif"
 [54] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_054_NDVI_FinalFitted.tif"
 [55] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_055_NDVI_FinalFitted.tif"
 [56] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_056_NDVI_FinalFitted.tif"
 [57] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_057_NDVI_FinalFitted.tif"
 [58] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_058_NDVI_FinalFitted.tif"
 [59] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_059_NDVI_FinalFitted.tif"
 [60] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_060_NDVI_FinalFitted.tif"
 [61] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_061_NDVI_FinalFitted.tif"
 [62] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_062_NDVI_FinalFitted.tif"
 [63] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_063_NDVI_FinalFitted.tif"
 [64] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_064_NDVI_FinalFitted.tif"
 [65] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_065_NDVI_FinalFitted.tif"
 [66] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_066_NDVI_FinalFitted.tif"
 [67] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_067_NDVI_FinalFitted.tif"
 [68] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_068_NDVI_FinalFitted.tif"
 [69] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_069_NDVI_FinalFitted.tif"
 [70] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_070_NDVI_FinalFitted.tif"
 [71] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_071_NDVI_FinalFitted.tif"
 [72] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_072_NDVI_FinalFitted.tif"
 [73] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_073_NDVI_FinalFitted.tif"
 [74] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_074_NDVI_FinalFitted.tif"
 [75] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_075_NDVI_FinalFitted.tif"
 [76] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_076_NDVI_FinalFitted.tif"
 [77] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_077_NDVI_FinalFitted.tif"
 [78] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_078_NDVI_FinalFitted.tif"
 [79] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_079_NDVI_FinalFitted.tif"
 [80] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_080_NDVI_FinalFitted.tif"
 [81] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_081_NDVI_FinalFitted.tif"
 [82] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_082_NDVI_FinalFitted.tif"
 [83] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_083_NDVI_FinalFitted.tif"
 [84] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_084_NDVI_FinalFitted.tif"
 [85] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_085_NDVI_FinalFitted.tif"
 [86] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_086_NDVI_FinalFitted.tif"
 [87] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_087_NDVI_FinalFitted.tif"
 [88] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_088_NDVI_FinalFitted.tif"
 [89] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_089_NDVI_FinalFitted.tif"
 [90] "raw_data/MODIS_TERRA/NDVI_FinalFitted/MOD09Q1_2013_090_NDVI_FinalFitted.tif"

In the code below: I am trying to subset the list and stack the rasters for only the first 31-days, but the code appears to take a while to load and I am unsure if I am doing this correctly. Can anyone verify that the code below is doing that? If it is not, how can I obtain only the first 31 files in rastlist to be stacked?

library(raster)
allrasters <- stack(rastlist)                       

#get the date from the names of the layers and extract the month
indices <- format(as.Date(names(allrasters), format = "MOD09Q1_%Y_%j"), format = "%j")
indices <- as.numeric(indices)

#sum layers
Jan_NDVI<- stackApply(allrasters, indices[1:31] ,fun = mean)

CodePudding user response:

We may extract the day after the year (assuming only a single year), convert to numeric class, order the rastlist and get the first 31 elements of the ordered vector

rastlist[order(as.integer(sub(".*_\\d{4}_(\\d{3})_.*", "\\1", rastlist)))[1:31]

If there are more than one year, then extract the date part, convert to Date class, order, and subset the ordered 'rastlist'

rastlist[order(as.Date(sub(".*_(\\d{4}_\\d{3})_.*", "\\1", 
          rastlist), "%Y_%j"))][1:31]
  • Related