st_apply apply a function to array dimensions: aggregate over space, time, or something else

# S3 method for stars
st_apply(
  X,
  MARGIN,
  FUN,
  ...,
  CLUSTER = NULL,
  PROGRESS = FALSE,
  FUTURE = FALSE,
  rename = TRUE
)

Arguments

X

object of class stars

MARGIN

see apply; index number(s) or name(s) of the dimensions over which FUN will be applied

FUN

see apply

...

arguments passed on to FUN

CLUSTER

cluster to use for parallel apply; see makeCluster

PROGRESS

logical; if TRUE, use pbapply::pbapply to show progress bar

FUTURE

logical;if TRUE, use future.apply::future_apply

rename

logical; if TRUE and X has only one attribute and FUN is a simple function name, rename the attribute of the returned object to the function name

Value

object of class stars with accordingly reduced number of dimensions; in case FUN returns more than one value, a new dimension is created carrying the name of the function used; see the examples.

Examples

tif = system.file("tif/L7_ETMs.tif", package = "stars") x = read_stars(tif) st_apply(x, 1:2, mean) # mean band value for each pixel
#> stars object with 2 dimensions and 1 attribute #> attribute(s): #> mean #> Min. : 25.50 #> 1st Qu.: 53.33 #> Median : 68.33 #> Mean : 68.91 #> 3rd Qu.: 82.00 #> Max. :255.00 #> dimension(s): #> from to offset delta refsys point values #> x 1 349 288776 28.5 +proj=utm +zone=25 +south... FALSE NULL [x] #> y 1 352 9120761 -28.5 +proj=utm +zone=25 +south... FALSE NULL [y]
st_apply(x, c("x", "y"), mean) # equivalent to the above
#> stars object with 2 dimensions and 1 attribute #> attribute(s): #> mean #> Min. : 25.50 #> 1st Qu.: 53.33 #> Median : 68.33 #> Mean : 68.91 #> 3rd Qu.: 82.00 #> Max. :255.00 #> dimension(s): #> from to offset delta refsys point values #> x 1 349 288776 28.5 +proj=utm +zone=25 +south... FALSE NULL [x] #> y 1 352 9120761 -28.5 +proj=utm +zone=25 +south... FALSE NULL [y]
st_apply(x, 3, mean) # mean of all pixels for each band
#> stars object with 1 dimensions and 1 attribute #> attribute(s): #> mean #> Min. :59.24 #> 1st Qu.:61.07 #> Median :65.97 #> Mean :68.91 #> 3rd Qu.:76.25 #> Max. :83.18 #> dimension(s): #> from to offset delta refsys point values #> band 1 6 NA NA NA NA NULL
st_apply(x, "band", mean) # equivalent to the above
#> stars object with 1 dimensions and 1 attribute #> attribute(s): #> mean #> Min. :59.24 #> 1st Qu.:61.07 #> Median :65.97 #> Mean :68.91 #> 3rd Qu.:76.25 #> Max. :83.18 #> dimension(s): #> from to offset delta refsys point values #> band 1 6 NA NA NA NA NULL
st_apply(x, 1:2, range) # min and max band value for each pixel
#> stars object with 3 dimensions and 1 attribute #> attribute(s): #> L7_ETMs.tif #> Min. : 1.00 #> 1st Qu.: 50.00 #> Median : 71.00 #> Mean : 72.44 #> 3rd Qu.: 96.00 #> Max. :255.00 #> dimension(s): #> from to offset delta refsys point values #> range 1 2 NA NA NA NA NULL #> x 1 349 288776 28.5 +proj=utm +zone=25 +south... FALSE NULL [x] #> y 1 352 9120761 -28.5 +proj=utm +zone=25 +south... FALSE NULL [y]
# to get a progress bar also in non-interactive mode, specify: if (require(pbapply)) { # install it, if FALSE pboptions(type = "timer") }
#> Loading required package: pbapply