Using if-function to apply SCL depending pixel mutation

Thanks Stefaan for your fast reply and your ideas/links - I will see if I can translate them it into R.

Update (just in case someone looks for a similar case):

Thanks also to the discussion on (cloud masking)

# load data cube with previously defined area of interest (aoi) and temporal extent (t)
cube_s2 <- p$load_collection(
  id = "SENTINEL2_L2A",
  spatial_extent = aoi,
  temporal_extent = t,
  bands=c("B02", "B03", "B04","B05","B06","B07","B08","B8A","B09","B11","B12","SCL")
)

# masking all bands according prefered SCL values to exclude e.g. clouded cells for later classification
cube_s2_mask <- p$apply(
  data = cube_s2,
  process = function(x, context) {
    scl = x["SCL"]
    scl_res = scl == 4 | scl == 5 | scl == 6
    p$`if`(value = scl_res, accept = x, reject = NULL)
  }
)

# reduce time dimension by setting median value and compute a median image for individual classification process
cube_s2_median_t <- p$reduce_dimension(data = cube_s2_mask,
                                       reducer = function(data, context) { p$median(data) },
                                       dimension = "t")