impute_factor {naniar} | R Documentation |
Impute a factor value into a vector with missing values
Description
For imputing fixed factor levels. It adds the new imputed value to the end
of the levels of the vector. We generally recommend to impute using other
model based approaches. See the simputation
package, for example
simputation::impute_lm()
.
Usage
impute_factor(x, value)
## Default S3 method:
impute_factor(x, value)
## S3 method for class 'factor'
impute_factor(x, value)
## S3 method for class 'character'
impute_factor(x, value)
## S3 method for class 'shade'
impute_factor(x, value)
Arguments
x |
vector |
value |
factor to impute |
Value
vector with a factor values replaced
Examples
vec <- factor(LETTERS[1:10])
vec[sample(1:10, 3)] <- NA
vec
impute_factor(vec, "wat")
library(dplyr)
dat <- tibble(
num = rnorm(10),
int = rpois(10, 5),
fct = factor(LETTERS[1:10])
) %>%
mutate(
across(
everything(),
\(x) set_prop_miss(x, prop = 0.25)
)
)
dat
dat %>%
nabular() %>%
mutate(
num = impute_fixed(num, -9999),
int = impute_zero(int),
fct = impute_factor(fct, "out")
)
[Package naniar version 1.1.0 Index]