impute_missing_values: Interpolate missing values using mean

impute_missing_valuesR Documentation

Interpolate missing values using mean

Description

impute_missing_values is a function that takes a numeric vector with missing data and replaces the NAs with the mean of the last value on the left side and the first non-NA value on the right. It does this only for repeated values of NA smaller than size max_na. This means that if max_na = 4, the function will only replace missing values if there are four or fewer missing values between the last non NA on the left and the first non NA on the right.

Usage

impute_missing_values(data, max_na = 4)

Arguments

data

Numeric vector with missing data

max_na

Maximum number of NA in a row and still fill in values

Value

Numeric vector with NA values in groups <= max_na filled in

Examples

## Not run: 
x <-
c(1, 1, NA, NA, NA, NA, 2, 2, 5, NA, NA, 6, 7, NA, 8, 9, NA,
  NA, NA, NA, NA, 10, 10, 13, NA, NA, 14, NA, NA, NA, NA, NA, NA, NA, 12)

impute_missing_values(data = x)


## End(Not run)

AndreSjuve/dretools documentation built on Dec. 4, 2024, 3:12 a.m.