% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/preproc_binning.R
\name{binning}
\alias{binning}
\title{Spectral data binning}
\usage{
binning(X, ppm = NULL, width = NULL, npoints = NULL, fun = sum)
}
\arguments{
\item{X}{Numeric matrix or data frame with spectra in rows, or a named list as
returned by \code{\link{read1d}}/\code{read1d_proc} containing \code{X} and \code{ppm}.}

\item{ppm}{Numeric vector of chemical shift positions (length must match \code{ncol(X)}).
If \code{NULL}, \code{ppm} is inferred in the following order:
\enumerate{
\item \code{X$ppm} if \code{X} is a list input,
\item \code{attr(X, "m8_axis")$ppm} (if present),
\item numeric \code{colnames(X)} (if present).
}}

\item{width}{Numeric. Bin size in ppm, or \code{NULL} if \code{npoints} is specified.}

\item{npoints}{Integer. Desired number of bins per spectrum, or \code{NULL} if \code{width} is specified.
If both are provided, \code{npoints} is used.}

\item{fun}{Function. Summary function applied to intensities within each bin.
Must return a single numeric value (e.g. \code{sum}, \code{mean}, \code{max}).}
}
\value{
Numeric matrix with spectra in rows and binned ppm variables in columns.
}
\description{
Equidistant binning of spectra by summarising intensities within ppm bins.
}
\details{
If present, preprocessing provenance is appended to \code{attr(X, "m8_prep")} using
\code{.m8_stamp()}. The ppm axis is updated in \code{attr(X, "m8_axis")$ppm} and
column names are set to the bin centres.

When \code{width} is specified, spectra are interpolated onto a regular ppm grid and
then aggregated within bins (\code{interp = TRUE} in provenance). When \code{npoints}
is specified, aggregation is performed by index bins on the original grid
(\code{interp = FALSE} in provenance).
}
\examples{
set.seed(1)
X <- matrix(rnorm(2 * 100), nrow = 2)
ppm <- round(seq(10, 0.5, length.out = 100), 3)
colnames(X) <- ppm

Xb <- binning(X, ppm, width = 0.5)
Xb_mean <- binning(X, ppm, width = 0.5, fun = mean)
dim(Xb)

}
\seealso{
Other preprocessing: 
\code{\link{align_segment}()},
\code{\link{align_spectra}()},
\code{\link{calibrate}()},
\code{\link{correct_baseline}()},
\code{\link{correct_lw}()},
\code{\link{pqn}()},
\code{\link{print_preprocessing}()}
}
\concept{preprocessing}
