Learn R Programming

raster (version 1.9-54)

aggregate: Aggregate cells or polygons

Description

Aggregate a Raster* object to create a new RasterLayer or RasterBrick with a lower resolution (larger cells). Aggregation groups rectangular areas to create larger cells. The value for the resulting cells is computed with a user-specified function. There is also an aggregate ('dissolve') method for SpatialPolygons that merges polygons if the values of selected variables are the same.

Usage

## S3 method for class 'Raster':
aggregate(x, fact=2, fun=mean, expand=TRUE, na.rm=TRUE, filename='', ...)

## S3 method for class 'SpatialPolygons':
aggregate(x, vars=NULL, sums, ...)

Arguments

x
Raster* object or SpatialPolygons object
fact
Integer. Aggregation factor expressed as number of cells in each direction (horizontally and vertically). Or two integers (horizontal and vertical aggregation factor). See Details
fun
Function used to aggregate values
expand
logical. If TRUE the output Raster* object will be larger then the input Raster* object if a division of the number of columns or rows with factor is not an integer
na.rm
logical. If TRUE, NA cells are removed from calculations
filename
Character. Output filename
...
Additional arguments as for writeRaster
vars
Character or integer. The variables (column names or numbers) that should be used to aggregate (dissolve) the SpatialPolygons by only maintaining unique combinations of these variables. The default setting is to use no variables and aggregate all polygons
sums
List with function(s) and variable(s) to summarize. This should be a list of lists in which each element of the main lists has two items. The first item is function (e.g. mean), the second element is a vector of column names (or indices) that need to summ

Value

  • RasterLayer or RasterBrick

Details

Aggregation will result in a Raster* object with fact*fact fewer cells; if necessary this number is adjusted according to the value of expand. For example, fact=2 will result in a new Raster* object with 2*2=4 times fewer cells. If two numbers are supplied, e.g., fact=c(2,3), the first will be used for aggregating in the horizontal direction, and the second for aggregating in the vertical direction, and the new RasterLayer will have 2*3=6 times fewer cells. Aggregation starts at the upper-left end of a raster. If a division of the number of columns or rows with factor does not return an integer, the extent of the resulting Raster object will either be somewhat smaller or somewhat larger then the original RasterLayer. For example, if an input RasterLayer has 100 columns, and fact=12, the output Raster object will have either 8 columns (expand=FALSE) (using 8 x 12 = 96 of the original columns) or 9 columns (expand=TRUE). In both cases, the maximum x coordinate of the output RasterLayer would, of course, also be adjusted. The function fun should take multiple numbers, and return a single number. For example mean, modal, min or max. It should also accept a na.rm argument (or ignore it as one of the 'dots' arguments).

See Also

disaggregate, resample

Examples

Run this code
r <- raster()
# a new aggregated raster, no values
ra <- aggregate(r, fact=10)
r <- setValues(r, runif(ncell(r)))
ra <- aggregate(r, fact=10, fun=max)
# a new aggregated raster, max of the values

Run the code above in your browser using DataLab