impute: Impute missing values

Description Usage Arguments Details Value See Also Examples

Description

impute returns a vector with missing values imputed.

Usage

1
impute(x, method, ...)

Arguments

x

A vector.

method

Either a value, a vector of values, or a function to use for imputation. See Details for more information.

...

Additional arguments for method when it is a function.

Details

impute returns the vector x with missing values imputed via method.

impute is designed to be readable from the function call. For example:

method can either be a value, a vector of values, or a function. Standard recycling is used if the vector of values is less than the number of NA values. When method is a function it takes only the non-NA values as input so that there is no need to supply those pesky na.rm=TRUE arguments. :)

Value

The output of impute is the vector x with missing values imputed.

See Also

Other imputers: impute_ecdf, impute_mode, impute_sample

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
x <- c(1, 1, 2, 3, NA, NA)

#####
# Common use cases
#

# I want to impute the mean!
x_mean <- impute(x, mean)
data.frame(x, x_mean)

# I want to impute missing values as -1 (e.g. for inclusion in a regression analysis)
x_m1 <- impute(x, -1)
data.frame(x, x_m1)

# I want to impute as "Missing"! (e.g. for inclusion in a regression analysis)
x_missing <- impute(x, "Missing")
data.frame(x, x_missing)

#####
# Using the impute helper functions
#

# I want to impute the most common category!
x_mode <- impute(x, impute_mode)
data.frame(x, x_mode)

# I want to impute from the empirical cdf!
x_ecdf_1 <- impute(x, impute_ecdf)
x_ecdf_2 <- impute(x, impute_ecdf)
data.frame(x, x_ecdf_1, x_ecdf_2)

# I want to impute using sampling with replacement!
x_sample_1 <- impute(x, impute_sample)
x_sample_2 <- impute(x, impute_sample)
data.frame(x, x_sample_1, x_sample_2)

derek-damron/transform documentation built on May 15, 2019, 3:57 a.m.