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:
dput(rastlist)
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",
"9609_pan_new.tif")
I managed to do it this for the first 8 elements of my list. Here's how:
setwd("mydir")
#first import all files in a single folder as a list
rlist <- list.files(path = "mydir", pattern='.tif$', all.files=T, full.names=F)
#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")
How can I generalize this?
Best Answer
Solved