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 )
| X, spc | a |
|---|---|
| MARGIN | The subscript which the function will be applied over.
If |
| FUN | function to compute the summary statistics |
| ... | further arguments passed to |
| label.wl, label.spc | new labels for wavelength and spectral intensity axes |
| new.wavelength | for |
| simplify | ignored: apply for hyperSpec results are always simplified |
A hyperSpec object
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.
base::apply(), for applying FUN to subgroups of
the hyperSpec object: aggregate().
C. Beleites
avgflu <- apply(flu, 1, mean, label.spc = expression(bar(I)), new.wavelength = mean(wl(flu)) ) avgflu#> hyperSpec object #> 6 spectra #> 3 data columns #> 1 data points / spectrumflu[[, , 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.52800apply(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