R/zApply.R

Defines functions zApply

Documented in zApply

# Oscar Perpinan Lamigueiro

zApply <- function(x, by, fun=mean, name='', ...){
	z <- getZ(x)
	stopifnot(length(z) == nlayers(x))

	##from aggregate.zoo
	my.unique <- function(x) x[match(x, x) == seq_len(length(x))] 
	my.sort <- function(x) x[order(x)]
	
    if (is.function(by)) { by <- by(z) }
    ##stopifnot(length(time(x)) == length(by))
    b <- stackApply(x, as.numeric(factor(by)), match.fun(fun), ...)
    zval <- my.sort(my.unique(by))
    b <- setZ(b, zval, name)
    names(b) <- as.character(zval)
    b
}

Try the raster package in your browser

Any scripts or data that you put into this service are public.

raster documentation built on April 6, 2019, 3:05 a.m.