# S4 method for hyperSpec sweep(x, MARGIN, STATS, FUN = "-", check.margin = TRUE, ...)
direction of the spectra matrix that
the summary statistic to sweep out. Either a vector or a
hyperSpec offers a non-standard convenience function: if
STATS is a
function, this function is applied first (with the same
compute the statistic. However, no further arguments to the apply
function can be given. See the examples.
the function to do the sweeping, e.g.
TRUE (the default), warn if the length or
STATS do not match the specified dimensions of
x. Set to
FALSE for a small speed gain when you
know that dimensions match.
further arguments for
base::sweep() for the spectra matrix.
sweep() is useful for some spectra pre-processing, like offset
correction, subtraction of background spectra, and normalization of the
## Subtract the background / slide / blank spectrum # the example data does not have spectra of the empty slide, # so instead the overall composition of the sample is subtracted background <- apply(faux_cell, 2, quantile, probs = 0.05) corrected <- sweep(faux_cell, 2, background, "-") plot(corrected, "spcprctl5") ## Offset correction offsets <- apply(faux_cell, 1, min) corrected <- sweep(faux_cell, 1, offsets, "-") plot(corrected, "spcprctl5") ## Min-max normalization (on max amide I) # the minimum is set to zero by the offset correction. factor <- apply(corrected, 1, max) mm.corrected <- sweep(corrected, 1, factor, "/") plot(mm.corrected, "spcprctl5") ## convenience: give function to compute STATS: mm.corrected2 <- sweep(corrected, 1, max, "/") plot(mm.corrected2) ## checking stopifnot(all(mm.corrected2 == mm.corrected))