Skip to contents

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)

# S3 method for default
impute_factor(x, value)

# S3 method for factor
impute_factor(x, value)

# S3 method for character
impute_factor(x, value)

# S3 method for 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
#>  [1] A    B    C    D    E    <NA> G    <NA> I    <NA>
#> Levels: A B C D E F G H I J

impute_factor(vec, "wat")
#>  [1] A   B   C   D   E   wat G   wat I   wat
#> Levels: A B C D E F G H I J 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
#> # A tibble: 10 × 3
#>        num   int fct  
#>      <dbl> <int> <fct>
#>  1  0.0742     4 A    
#>  2 -0.201     NA B    
#>  3  1.51      NA C    
#>  4 NA          4 D    
#>  5 NA          3 E    
#>  6  1.37       6 NA   
#>  7  1.06       3 G    
#>  8 -1.00       3 NA   
#>  9  0.880      4 I    
#> 10  0.987      7 J    

dat %>%
  nabular() %>%
  mutate(
    num = impute_fixed(num, -9999),
    int = impute_zero(int),
    fct = impute_factor(fct, "out")
  )
#> # A tibble: 10 × 6
#>           num   int fct   num_NA int_NA fct_NA
#>         <dbl> <dbl> <fct> <fct>  <fct>  <fct> 
#>  1     0.0742     4 A     !NA    !NA    !NA   
#>  2    -0.201      0 B     !NA    NA     !NA   
#>  3     1.51       0 C     !NA    NA     !NA   
#>  4 -9999          4 D     NA     !NA    !NA   
#>  5 -9999          3 E     NA     !NA    !NA   
#>  6     1.37       6 out   !NA    !NA    NA    
#>  7     1.06       3 G     !NA    !NA    !NA   
#>  8    -1.00       3 out   !NA    !NA    NA    
#>  9     0.880      4 I     !NA    !NA    !NA   
#> 10     0.987      7 J     !NA    !NA    !NA