Replace NA by Aggregation
na.aggregate.RdGeneric function for replacing each NA with aggregated
values. This allows imputing by the overall mean, by monthly means,
etc.
Usage
na.aggregate(object, ...)
# Default S3 method
na.aggregate(object, by = 1, ..., FUN = mean,
na.rm = FALSE, maxgap = Inf)Arguments
- object
an object.
- by
a grouping variable corresponding to
object, or a function to be applied totime(object)to generate the groups.- ...
further arguments passed to
byifbyis a function.- FUN
function to apply to the non-missing values in each group defined by
by.- na.rm
logical. Should any remaining
NAs be removed?- maxgap
maximum number of consecutive
NAs to fill. Any longer gaps will be left unchanged.
Value
An object in which each NA in the input object is replaced
by the mean (or other function) of its group, defined by
by. This is done for each series in a multi-column object. Common
choices for the aggregation group are a year, a month, all calendar
months, etc.
If a group has no non-missing values, the default aggregation function
mean will return NaN. Specify na.rm = TRUE to
omit such remaining missing values.
Examples
z <- zoo(c(1, NA, 3:9),
c(as.Date("2010-01-01") + 0:2,
as.Date("2010-02-01") + 0:2,
as.Date("2011-01-01") + 0:2))
## overall mean
na.aggregate(z)
#> 2010-01-01 2010-01-02 2010-01-03 2010-02-01 2010-02-02 2010-02-03 2011-01-01
#> 1.000 5.375 3.000 4.000 5.000 6.000 7.000
#> 2011-01-02 2011-01-03
#> 8.000 9.000
## group by months
na.aggregate(z, as.yearmon)
#> 2010-01-01 2010-01-02 2010-01-03 2010-02-01 2010-02-02 2010-02-03 2011-01-01
#> 1 2 3 4 5 6 7
#> 2011-01-02 2011-01-03
#> 8 9
## group by calendar months
na.aggregate(z, months)
#> 2010-01-01 2010-01-02 2010-01-03 2010-02-01 2010-02-02 2010-02-03 2011-01-01
#> 1.0 5.6 3.0 4.0 5.0 6.0 7.0
#> 2011-01-02 2011-01-03
#> 8.0 9.0
## group by years
na.aggregate(z, format, "%Y")
#> 2010-01-01 2010-01-02 2010-01-03 2010-02-01 2010-02-02 2010-02-03 2011-01-01
#> 1.0 3.8 3.0 4.0 5.0 6.0 7.0
#> 2011-01-02 2011-01-03
#> 8.0 9.0