This can be useful if you are imputing specific values, however we would generally recommend to impute using other model based approaches. See the simputation package, for example simputation::impute_lm().

impute_fixed(x, value)

# Default S3 method
impute_fixed(x, value)

Arguments

x

vector

value

value to impute

Value

vector with a fixed values replaced

Examples


vec <- rnorm(10)

vec[sample(1:10, 3)] <- NA

vec
#>  [1]  2.4815984 -1.1383021         NA -0.6310333  0.5363818 -1.4013999
#>  [7]  1.5176090         NA         NA  0.3987200

impute_fixed(vec, -999)
#>  [1]    2.4815984   -1.1383021 -999.0000000   -0.6310333    0.5363818
#>  [6]   -1.4013999    1.5176090 -999.0000000 -999.0000000    0.3987200

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 NA          1 A    
#>  2 NA         NA B    
#>  3 -0.813     NA C    
#>  4 -0.0584     1 NA   
#>  5 -2.26       9 E    
#>  6 -1.14       7 F    
#>  7 -0.294      2 G    
#>  8 -0.493      5 NA   
#>  9  1.95       7 I    
#> 10  0.349      3 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 -9999          1 A     NA     !NA    !NA   
#>  2 -9999          0 B     NA     NA     !NA   
#>  3    -0.813      0 C     !NA    NA     !NA   
#>  4    -0.0584     1 out   !NA    !NA    NA    
#>  5    -2.26       9 E     !NA    !NA    !NA   
#>  6    -1.14       7 F     !NA    !NA    !NA   
#>  7    -0.294      2 G     !NA    !NA    !NA   
#>  8    -0.493      5 out   !NA    !NA    NA    
#>  9     1.95       7 I     !NA    !NA    !NA   
#> 10     0.349      3 J     !NA    !NA    !NA