na_aggregate: Replace NAs by aggregation

Description Usage Arguments Value See Also Examples

View source: R/na_aggregate.R

Description

Generic function for replacing each NA with aggregated values. This allows imputing by the overall mean, by monthly means, etc.

Usage

1
na_aggregate(object, ...)

Arguments

object

an object.

...

further arguments passed to by if by is a function.

by

a grouping variable corresponding to object, or a function to be applied to time(object) to generate the groups.

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.

See Also

na_mean(), na_max(), na_median(), etc. for similar

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
  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)
  ## group by months
  na_aggregate(z, as.yearmon)
  ## group by calendar months
  na_aggregate(z, months)
  ## group by years
  na_aggregate(z, format, "%Y")

decisionpatterns/na.actions documentation built on Aug. 25, 2020, 8:04 p.m.