Impute the mode value into a vector with missing values

impute_mode(x)

# Default S3 method
impute_mode(x)

# S3 method for class 'integer'
impute_mode(x)

# S3 method for class 'factor'
impute_mode(x)

Arguments

x

vector

This approach adapts examples provided from stack overflow, and for the integer case, just rounds the value. While 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().

Value

vector with mode values replaced

Examples


vec <- rnorm(10)

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

impute_mode(vec)
#>  [1]  1.1205255  2.3226911  1.5123997  1.5123997  1.5123997  1.6426116
#>  [7] -0.1871393  1.6418184  1.3258936 -0.4010212

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 -1.34      NA A    
#>  2 -0.408      4 B    
#>  3  0.460      5 C    
#>  4 NA         NA D    
#>  5  1.32       0 E    
#>  6 -1.08       7 F    
#>  7 -0.0212     2 G    
#>  8 -0.331      9 NA   
#>  9 NA          5 I    
#> 10 -0.807      9 NA   


dat %>%
  nabular() %>%
  mutate(
    num = impute_mode(num),
    int = impute_mode(int),
    fct = impute_mode(fct)
  )
#> # A tibble: 10 × 6
#>        num   int fct   num_NA int_NA fct_NA
#>      <dbl> <dbl> <fct> <fct>  <fct>  <fct> 
#>  1 -1.34       5 A     !NA    NA     !NA   
#>  2 -0.408      4 B     !NA    !NA    !NA   
#>  3  0.460      5 C     !NA    !NA    !NA   
#>  4 -0.511      5 D     NA     NA     !NA   
#>  5  1.32       0 E     !NA    !NA    !NA   
#>  6 -1.08       7 F     !NA    !NA    !NA   
#>  7 -0.0212     2 G     !NA    !NA    !NA   
#>  8 -0.331      9 G     !NA    !NA    NA    
#>  9 -0.511      5 I     NA     !NA    !NA   
#> 10 -0.807      9 G     !NA    !NA    NA