Learn R Programming

ursa (version 3.9.4)

zonal_stat: Zonal statistics for raster maps

Description

'Zonal' operator of map algebra. Applied to raster images.

Usage

zonal_stat(x, by, FUN, table = FALSE)

# S3 method for ursaRaster aggregate(x, by, FUN, table = FALSE, ...)

Arguments

x

ursaRaster object. Image for analysis.

by

ursaRaster object. Image of grouping elements.

FUN

a function to compute the summary statistics which can be applied to all data subsets.

table

Logical. If table=TRUE then summary statistics for each group is returned. The statistics is defined by FUN. If stat=FALSE then result is presented as ursaRaster object.

Other arguments which passed to function aggregate of package stats

Value

If table=FALSE then ursaRaster object of summarized statistics.

If table=TRUE then data.frame.

Details

zonal_stat is a wrapper of aggregate(x,by,FUN,table=FALSE,na.rm=TRUE)

You can use multichannel image (argument x) for analysis.

You can use multichannel raster image for group elements (argument by)

Examples

Run this code
# NOT RUN {
session_grid(NULL)
session_grid(regrid(mul=1/2))
a <- pixelsize()
val <- c(normal=a,half=a/2)
gr <- c(group=colorize(a,nbreak=1))#+0
print(as.table(gr))
##~ display(gr)
ra <- round(aggregate(val,gr,mean),4)
print(ra)
print(as.table(ra[1]))
print(as.table(ra[2]))
da <- aggregate(val,gr,table=TRUE,mean)
n <- aggregate(a,gr,table=TRUE,length)[,2,drop=FALSE]
da <- cbind(da,n=unname(n))
gr2 <- c(group2=colorize(a,nbreak=6))#+0
mgr <- list(gr,gr2)
da2 <- aggregate(val[1],mgr,table=TRUE,mean)
print(da2)
da3 <- aggregate(val,mgr,table=TRUE,mean)
print(da3)
ra3 <- aggregate(val,mgr,table=FALSE,mean) ## not implemented for rasters
print(ra3)
# }

Run the code above in your browser using DataLab