Learn R Programming

COINr (version 1.1.14)

i_mean_grp: Impute by group mean

Description

Replaces NAs in a numeric vector with the grouped arithmetic means of the non-NA values. Groups are defined by the f argument.

Usage

i_mean_grp(x, f, skip_f_na = TRUE)

Value

A numeric vector

Arguments

x

A numeric vector

f

A grouping variable, of the same length of x, that specifies the group that each value of x belongs to. This will be coerced to a factor.

skip_f_na

If TRUE, will work around any NAs in f (the corresponding values of x will be excluded from the imputation and returned unaltered). Else if FALSE, will cause an error.

Examples

Run this code
x <- c(NA, runif(10), NA)
f <- c(rep("a", 6), rep("b", 6))
i_mean_grp(x, f)

Run the code above in your browser using DataLab