Examples for 'stars::st_apply'


st_apply apply a function to one or more array dimensions

Aliases: st_apply st_apply.stars

Keywords:

### ** 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):
      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]
st_apply(x, c("x", "y"), mean) # equivalent to the above
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]
st_apply(x, 3, mean)   # mean of all pixels for each band
stars object with 1 dimensions and 1 attribute
attribute(s):
          Min.  1st Qu.   Median     Mean  3rd Qu.     Max.
mean  59.23541 61.07112 65.96675 68.91242 76.25445 83.18266
dimension(s):
     from to
band    1  6
## Not run: 
##D  st_apply(x, "band", mean) # equivalent to the above
##D  st_apply(x, 1:2, range) # min and max band value for each pixel
##D  fn_ndvi1 = function(x) (x[4]-x[3])/(x[4]+x[3]) # ONE argument: will be called for each pixel
##D  fn_ndvi2 = function(red,nir) (nir-red)/(nir+red) # n arguments: will be called only once
##D  ndvi1 = st_apply(x, 1:2, fn_ndvi1)
##D    # note that we can select bands 3 and 4 in the first argument:
##D  ndvi2 = st_apply(x[,,,3:4], 1:2, fn_ndvi2) 
##D  all.equal(ndvi1, ndvi2)
##D  # compute the (spatial) variance of each band; https://github.com/r-spatial/stars/issues/430
##D  st_apply(x, 3, function(x) var(as.vector(x))) # as.vector is required!
##D  # to get a progress bar also in non-interactive mode, specify:
##D  if (require(pbapply)) { # install it, if FALSE
##D    pboptions(type = "timer")
##D  }
##D  st_apply(x, 1:2, range) # dimension "range" is first; rearrange by:
##D  st_apply(x, 1:2, range) %>% aperm(c(2,3,1))
## End(Not run)

[Package stars version 0.6-7 Index]