# zonal.stats

0th

Percentile

##### zonal.stats

Polygon zonal statistics of a raster

##### Usage
zonal.stats(x, y, stats = c("min", "mean", "max"))
##### Arguments
x

Polygon object of class SpatialPolygonsDataFrame

y

rasterLayer object of class raster

stats

Statistic or function

##### Value

data.frame, nrow(x) and ncol of function results

##### Note

This function calculates the zonal statistics between a polygon vector object and a raster. This provides the advantage of being able to accept any custom function, passed to the 'stats' argument. Please note that any custom function needs to have a 'na.rm' argument.

• zonal.stats
##### Examples
# NOT RUN {
library(raster)
library(sp)

# skewness function
skew <- function(x, na.rm = FALSE) {
if (na.rm)
x <- x[!is.na(x)]
sum( (x - mean(x)) ^ 3) / ( length(x) * sd(x) ^ 3 )
}

# percent x >= p function
pct <- function(x, p=0.30, na.rm = FALSE) {
if ( length(x[x >= p]) < 1 )  return(0)
if ( length(x[x >= p]) == length(x) ) return(1)
else return( length(x[x >= p]) / length(x) )
}

# create some example data
p <- raster(nrow=10, ncol=10)
p[] <- runif(ncell(p)) * 10
p <- rasterToPolygons(p, fun=function(x){x > 9})
r <- raster(nrow=100, ncol=100)
r[] <- runif(ncell(r))
plot(r)

# run zonal statistics using skew and pct functions
z.skew <- zonal.stats(x = p, y = r, stats = "skew")
z.pct <- zonal.stats(x=p, y=r, stats = "pct")
( z <- data.frame(ID = as.numeric(as.character(row.names(p@data))),
SKEW=z.skew, PCT=z.pct) )

# }

Documentation reproduced from package spatialEco, version 1.3-2, License: GPL-3

### Community examples

Looks like there are no examples yet.