How to create a function that merges the bands for each ID to create two multi-band TIFFs in R?

I have 16 separate raster files, named by ID and band, for 8 bands and 2 IDs. How can I merge the bands for each ID to create two multi-band TIFFs? Here are my data:

    c("7398_b10_new.tif", "7398_b2_clip.tif", "7398_b3_clip.tif", 
"7398_b4_clip.tif", "7398_b5_clip.tif", "7398_b6_clip.tif", "7398_b7_clip.tif", 
"7398_pan_new.tif", "9609_b10_new.tif", "9609_b2_clip.tif", "9609_b3_clip.tif", 
"9609_b4_clip.tif", "9609_b5_clip.tif", "9609_b6_clip.tif", "9609_b7_clip.tif", 

I managed to do it this for the first 8 elements of my list. Here's how:


#first import all files in a single folder as a list 
rlist <- list.files(path = "mydir", pattern='.tif

#substract the  first 8 raster of the list
n = tail(rlist,8)

#stack layers
rstack = stack(n)

#substract the first element of the list
one = tail(n, 1)

#get the first 4 letters of the first element of a list (to be used as raster name of the raster stack)
rexport = substr(one, 1, 4)

writeRaster(rstack, filename = rexport, options = "INTERLEAVE=BAND", overwrite = T, format = "GTiff")

I am trying to create a function that takes a list of raster, performs the above code in the first 8 raster, then continues to next 8 raster etc.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.