Learn R Programming

rts (version 1.1-14)

period.apply: Apply a function over specified time intervals

Description

Apply specified function over each period of date/time defined in INDEX at each grid cell.

Usage

period.apply(x, INDEX, FUN, ...)

Value

RasterStackTS or RasterBrickTS

Arguments

x

a raster time series (Raster*TS) object created by rts

INDEX

a numeric vector of endpoints of time/date periods to apply function over

FUN

an argument of type function

...

additional arguments for FUN

Details

This functions subsets the raster data based on the specified time periods (endpoint for each period should be specified in INDEX), and FUN function will be applied to the subsetted values at each grid cell for each period. For each period, a raster will be calculated and the end of the date/time period will be assigned to it in the output raster time series object. If the INDEX is out of range, the function stops working and an error is generated.

See Also

Examples

Run this code
if (FALSE) {
file <- system.file("external/ndvi", package="rts")

ndvi <- rts(file) # read the ndvi time series from the specified file

ndvi

ep <- endpoints(ndvi,'years') # extract the end index on each year period

ndvi.y <- period.apply(ndvi,ep,mean) # apply the mean function on each year

ndvi.y

#---------
ep <- endpoints(ndvi,'quarters') # extract the end index on each quarter of a year

# a function:
f <- function(x) {
  if (min(x) > 0.5) mean(x)
  else 0
}

ndvi.q <- period.apply(ndvi,ep,f) # apply the function f on each quarter

}

Run the code above in your browser using DataLab