Average the values of a vector f
in bins defined on another
vector x
. The values are broken up into bins using cut()
.
binMean1D(x, f, xbreaks, include.lowest = FALSE, na.rm = FALSE)
vector of numerical values that will be categorized into
bins via the xbreaks
parameter.
vector of numerical values that are associated with the x
values.
vector of values of x
at the boundaries between bins, calculated using
pretty()
if not supplied.
logical value indicating whether to include
x
values that equal xbreaks[1]
. See “Details”.
logical value indicating whether to remove NA values before
doing the computation of the average. This is passed to mean()
, which
does the work of the present function.
A list with the following elements: the breaks (xbreaks
,
midpoints (xmids
) between those breaks,
the count (number
) of x
values between successive breaks,
and the resultant average (result
) of f
, classified by the
x
breaks.
By default, the sub-intervals defined by the xbreaks
argument are open
on the left and closed on the right, to match the behaviour
of cut()
. An open interval does not include points on
the boundary, and so any x
values that exactly match
the first breaks
value will not be counted. To include
such points in the calculation, set include.lowest
to TRUE.
Other bin-related functions:
binApply1D()
,
binApply2D()
,
binAverage()
,
binCount1D()
,
binCount2D()
,
binMean2D()
# Plot raw temperature profile as circles, with lines indicating
# the result of averaging in 1-metre depth intervals.
library(oce)
data(ctd)
z <- ctd[["z"]]
T <- ctd[["temperature"]]
plot(T, z, cex = 0.3)
TT <- binMean1D(z, T, seq(-100, 0, 1))
lines(TT$result, TT$xmids, col = rgb(1, 0, 0, 0.9), lwd = 2)