apply() gives the functionality of base::apply() for hyperSpec objects.

# S4 method for hyperSpec
apply(
  X,
  MARGIN,
  FUN,
  ...,
  label.wl = NULL,
  label.spc = NULL,
  new.wavelength = NULL,
  simplify
)

Arguments

X, spc

a hyperSpec object

MARGIN

The subscript which the function will be applied over.

  • 1 indicates rows (FUN is applied to each spectrum),

  • 2 indicates columns (FUN is applied to each wavelength),

  • 1:2 indicates that FUN should be applied to each single element of the spectra matrix. Note that many basic mathematical functions are already defined for hyperSpec objects (see Math()).

If MARGIN is missing, the whole spectra matrix is handed to FUN, see also the examples.

FUN

function to compute the summary statistics

...

further arguments passed to FUN

label.wl, label.spc

new labels for wavelength and spectral intensity axes

new.wavelength

for MARGIN = 2: numeric vector or name of the argument in ... that is to be used (character) as wavelength axis of the resulting object.

simplify

ignored: apply for hyperSpec results are always simplified

Value

A hyperSpec object

Details

The generic functions of group methods::Math() are not defined for hyperSpec objects. Instead, apply can be used. For functions like log() that work on scalars, MARGIN = 1:2 gives the appropriate behavior.

spcapply does the same as apply with MARGIN = 1, but additionally allows to set a new wavelength axis and adjust the labels.

wlapply does the same as apply with MARGIN = 2, but additionally allows to set a new wavelength axis and adjust the labels.

See also

base::apply(), for applying FUN to subgroups of the hyperSpec object: aggregate().

Author

C. Beleites

Examples



plot_spc(apply(faux_cell, 2, range))


avg_flu <- apply(flu, 1, mean,
  label.spc = expression(bar(I)),
  new.wavelength = mean(wl(flu))
)
avg_flu
#> hyperSpec object
#>    6 spectra
#>    3 data columns
#>    1 data points / spectrum

flu[[, , 405:407]]
#>            405       406       407
#> [1,]  27.15000  33.37867  36.53133
#> [2,]  66.80133  66.71200  72.52967
#> [3,]  93.14433 106.19367 113.24867
#> [4,] 130.66367 143.79767 152.13267
#> [5,] 167.26667 177.47067 189.75233
#> [6,] 198.43033 215.78500 232.52800
apply(flu, 1:2, "*", -1)[[, , 405:407]]
#>            [,1]       [,2]       [,3]
#> [1,]  -27.15000  -33.37867  -36.53133
#> [2,]  -66.80133  -66.71200  -72.52967
#> [3,]  -93.14433 -106.19367 -113.24867
#> [4,] -130.66367 -143.79767 -152.13267
#> [5,] -167.26667 -177.47067 -189.75233
#> [6,] -198.43033 -215.78500 -232.52800

## without MARGIN the whole matrix is handed to FUN
apply(flu[, , 405:407], , print)[[]]
#>            405       406       407
#> [1,]  27.15000  33.37867  36.53133
#> [2,]  66.80133  66.71200  72.52967
#> [3,]  93.14433 106.19367 113.24867
#> [4,] 130.66367 143.79767 152.13267
#> [5,] 167.26667 177.47067 189.75233
#> [6,] 198.43033 215.78500 232.52800
#>            405       406       407
#> [1,]  27.15000  33.37867  36.53133
#> [2,]  66.80133  66.71200  72.52967
#> [3,]  93.14433 106.19367 113.24867
#> [4,] 130.66367 143.79767 152.13267
#> [5,] 167.26667 177.47067 189.75233
#> [6,] 198.43033 215.78500 232.52800

## whereas MARGIN = 1 : 2 leads to FUN being called
## for each element separately
apply(flu[, , 405:407], 1:2, print)[[]]
#> [1] 27.15
#> [1] 66.80133
#> [1] 93.14433
#> [1] 130.6637
#> [1] 167.2667
#> [1] 198.4303
#> [1] 33.37867
#> [1] 66.712
#> [1] 106.1937
#> [1] 143.7977
#> [1] 177.4707
#> [1] 215.785
#> [1] 36.53133
#> [1] 72.52967
#> [1] 113.2487
#> [1] 152.1327
#> [1] 189.7523
#> [1] 232.528
#>           [,1]      [,2]      [,3]
#> [1,]  27.15000  33.37867  36.53133
#> [2,]  66.80133  66.71200  72.52967
#> [3,]  93.14433 106.19367 113.24867
#> [4,] 130.66367 143.79767 152.13267
#> [5,] 167.26667 177.47067 189.75233
#> [6,] 198.43033 215.78500 232.52800