I have a set of rasters. I crop them all using FOR loop. Then, I want to save new-created rasters in new directory, not in my actual directory. For one raster it is easy:
writeRaster(raster1, filename="raster1.img", format="HFA", overwrite=TRUE)
script for my loops:
# read files
setwd("D:/Data/LANDSAT")
library(raster)
library(rgdal)
# read files containing n_msi...
raster_data<-list.files(path=getwd(), pattern="n_msi.*.img$")
a<-raster("a.img")
# create a list object containing raster objects
rast.list<-list()
for(i in 1:length(raster_data)){
rast.list[i] <-raster(raster_data[i])}
# get extent of smaller raster
e<-extent(a)
# crop larger raster to smaller raster - it create rectangle object
crop.list<-list()
for(i in 1:length(rast.list)){
crop.list[[i]]<-crop(rast.list[[i]], e)}
# get rasters names
rst.names<-c(1:length(rast.list))
for(i in 1:length(rast.list)){
rst.names1<-names(rast.list[[i]])
rst.names[[i]]<-rst.names1}
# write raster data
mapply(writeRaster, crop.list, new.rst.names, 'HFA')
Please, how to write "mapply" to save rasters in new directory? or, how to loop it with "writeRasters"?