7.13 Aggregation

Dimension-wise, we can apply functions to selected array dimensions

st_apply(r, c("x", "y"), mean)
## stars object with 2 dimensions and 1 attribute
## attribute(s):
##       Min.  1st Qu.   Median     Mean 3rd Qu. Max.
## mean  25.5 53.33333 68.33333 68.91242      82  255
## dimension(s):
##   from  to  offset delta                     refsys point x/y
## x    1 349  288776  28.5 SIRGAS 2000 / UTM zone 25S FALSE [x]
## y    1 352 9120761 -28.5 SIRGAS 2000 / UTM zone 25S FALSE [y]

Example: NDVI (normalised differenced vegetation index)

ndvi <- function(b1, b2, b3, b4, b5, b6) (b4 - b3)/(b4 + b3)
st_apply(r, c("x", "y"), ndvi)
## stars object with 2 dimensions and 1 attribute
## attribute(s):
##             Min.    1st Qu.      Median        Mean   3rd Qu.      Max.
## ndvi  -0.7534247 -0.2030075 -0.06870229 -0.06432464 0.1866667 0.5866667
## dimension(s):
##   from  to  offset delta                     refsys point x/y
## x    1 349  288776  28.5 SIRGAS 2000 / UTM zone 25S FALSE [x]
## y    1 352 9120761 -28.5 SIRGAS 2000 / UTM zone 25S FALSE [y]

Aggregation over a temporal dimension is done by passing a time variable as the second argument to aggregate, as a

  • set of time stamps indicating the start of time intervals,
  • set of time intervals defined by make_intervals, or
  • time period like "weeks", "5 days", or "years".