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

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

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.

Value

RasterStackTS or RasterBrickTS

Author(s)

Babak Naimi

naimi.b@gmail.com

http://r-gis.net

See Also

period.apply

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
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