Here is an example of how to do this. (link to original)
#########################################################################
# SubsampleImageRaster.r
#
# This function demonstrates resampling of raster images to a new
# spatial resolution using the R raster package.
#
# Author: Rick Reeves
# Date created: 6-October- 2010
# Date modified:
# NCEAS
#
#########################################################################
#
SubsampleImageRaster <- function()
{
library(raster)
sOutFile <- ""
resampleFactor <- 4 # For test, subsample incoming image by factor of 10
# Read the mosaic components, stored in a subfolder, into a raster object list.
# Within the same loop, obtain the (geographic) extent of each component.
# Note: these images do not have same spatial extent, so they cant be stored
# in a rasterStack. Instead, use a list of rasterLayers.
setwd("./ForUseCase")
inFiles <- list.files(pattern="*.tif")
nFiles <- length(inFiles)
inputRaster <- raster()
CoarseResampRaster <- raster()
FineResampRaster <- raster()
for (iCtr in 1 : nFiles)
{
message(sprintf("resampling file: %s",inFiles[iCtr]))
inputRaster <- raster(inFiles[iCtr])
# The aggregate() / disaggregate methods resample rasters to COARSER (bigger cells)
# and FINER (smaller cells) resolutions, respectively
CoarseResampRaster <- aggregate(inputRaster,fact=resampleFactor,fun=mean)
sOutFile <- sprintf("CoarseSubsamp%s",inFiles[iCtr])
writeRaster(CoarseResampRaster,filename=sOutFile,format="GTiff",datatype="INT1U",overwrite=TRUE)
FineResampRaster <- disaggregate(inputRaster,fact=resampleFactor,fun=mean)
sOutFile <- sprintf("FineSubsamp%s",inFiles[iCtr])
writeRaster(FineResampRaster,filename=sOutFile,format="GTiff",datatype="INT1U",overwrite=TRUE)
}
message("resample demo")
browser()
# second method: use the resample() method from raster package
# Simple example:
# This code produces a resampled raster, 's',
# with correct resampled values. e.g.;
# s[] prints a vector of resampled cell values.
r <- raster(nrow=3, ncol=3)
r[] <- 1:ncell(r)
s <- raster(nrow=10, ncol=10)
s <- resample(r, s, method='bilinear')
# Useful example:
# Resample a satellite image, stored in a GeoTiff file
# into a NEW raster with 2x spatial resolution in
# both dimensions (four times the number of cells)
# Here is the technique:
# 1) Create a new raster object with the correct 'resampled' number of cells.
# 2) Set the extent (geographic 'bounding box') of the new raster
# to the extent of the original raster
# 3) Generate the resampled raster.
resampleFactor <- .5 # reduce the cell size by 50% and double the number of rows and columns.
inputRaster <- raster("TmB50MosaicImg1.tif")
inCols <- ncol(inputRaster)
inRows <- nrow(inputRaster)
resampledRaster <- raster(ncol=(inCols / resampleFactor), nrow=(inRows / resampleFactor))
extent(resampledRaster) <- extent(inputRaster)
# The resample method will write the resampled raster image to a NEW disk file..
resampledRaster <- resample(inputRaster,resampledRaster,datatype="INT1U",method='bilinear',filename="testOutResamp.tif",overwrite=TRUE)
# Or, use writeRaster method to create the output file.
writeRaster(resampledRaster,filename="ResampleProduct.tif",format="GTiff",datatype="INT1U",overwrite=TRUE)
# end
}