timeWeightedMean: Time-Weighted Mean

View source: R/timeWeightedMean.R

timeWeightedMeanR Documentation

Time-Weighted Mean

Description

Computes the mean weighted by the duration between values.

Usage

timeWeightedMean(x, time, na.rm = TRUE, fill = TRUE, by.period = FALSE,
  excessive = 2.5)

Arguments

x

a sequnece of numeric values whose mean is to be computed.

time

the times associated with x. See Details.

na.rm

remove missing values before computing the mean?

fill

expand the weights for the first and last observation to the end of the period? Otherwise, the weight is based on the distance to the second or next-to-last observation.

by.period

computes wights by the periods defined by time.

excessive

a warning is issued if the largest weight for any observation exceeds excessive times the average weight.

Details

The values of time are expected to be in decimal format, where the integer part indicates the period and the fractional part linearly distributed through the period. For example, the year 2000 begins at 2,000.0 and July 2, 2000 is 2,000.5. The function dectime can be used to convert Date or POSIX to decimal format. If time is of class "Date," or any "POSIX" classs, it is converted using dectime.
If na.rm is FALSE and byeriod is TRUE, then missing values are returned only for periods that contain a missing value (NA).

Value

If by.period is TRUE, then a vector with one entry per period, otherwise the mean for the entire data set.

References

Crawford,C.G., 2004, Sampling strategies for estimating acute and chronic exposures of pesticides in streams: Journal of the American Water Resources Association, v. 40, n. 2, p 485-502.

See Also

weighted.mean, dectime

Examples

## Not run: 
library(smwrData)
data(QW05078470)
with(QW05078470, timeWeightedMean(P00665, dectime(DATES)))

## End(Not run)

USGS-R/smwrStats documentation built on Oct. 11, 2022, 6:15 a.m.