Predictive Hacks

How to Impute Missing Values in R

impute NA

In the real data world, it is quite common to deal with Missing Values (known as NAs). Sometimes, there is a need to impute the missing values where the most common approaches are:

  • Numerical Data: Impute Missing Values with mean or median
  • Categorical Data: Impute Missing Values with mode

Let’s give an example of how we can impute dynamically depending on the data type.


df<-tibble(id=seq(1,10), ColumnA=c(10,9,8,7,NA,NA,20,15,12,NA), 

# A tibble: 10 x 5
      id ColumnA ColumnB ColumnC ColumnD
   <int>   <dbl> <fct>   <fct>     <dbl>
 1     1      10 "A"     ""           NA
 2     2       9 "B"     "BB"         20
 3     3       8 "A"     "CC"         18
 4     4       7 "A"     "BB"         22
 5     5      NA ""      "BB"         18
 6     6      NA "B"     "CC"         17
 7     7      20 "A"     "AA"         19
 8     8      15 "B"     "BB"         NA
 9     9      12 ""      ""           17
10    10      NA "A"     "AA"         23

For the Categorical Variables, we are going to apply the “mode” function which we have to build it since it is not provided by R.

getmode <- function(v){
  uniqv <- unique(v)
  uniqv[which.max(tabulate(match(v, uniqv)))]

Now that we have the “mode” function we are ready to impute the missing values of a dataframe depending on the data type of the columns. Thus, if the column data type is “numeric” we will impute it with the “mean” otherwise with the “mode“. Notice that in our script we take into account the column names and “dplyr” package requires a special notation (!!cols : = !!rlang::sym(colname)) of selecting dynamically the column names.

for (cols in colnames(df)) {
  if (cols %in% names(df[,sapply(df, is.numeric)])) {
    df<-df%>%mutate(!!cols := replace(!!rlang::sym(cols),!!rlang::sym(cols)), mean(!!rlang::sym(cols), na.rm=TRUE)))
  else {
    df<-df%>%mutate(!!cols := replace(!!rlang::sym(cols), !!rlang::sym(cols)=="", getmode(!!rlang::sym(cols))))

> df
# A tibble: 10 x 5
      id ColumnA ColumnB ColumnC ColumnD
   <dbl>   <dbl> <fct>   <fct>     <dbl>
 1     1    10   A       BB         19.2
 2     2     9   B       BB         20  
 3     3     8   A       CC         18  
 4     4     7   A       BB         22  
 5     5    11.6 A       BB         18  
 6     6    11.6 B       CC         17  
 7     7    20   A       AA         19  
 8     8    15   B       BB         19.2
 9     9    12   A       BB         17  
10    10    11.6 A       AA         23  

Voilà! The missing values have been imputed!

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

2 thoughts on “How to Impute Missing Values in R”

  1. I would encourage you to look into multiple imputation (the mice package is really good). Mean and mode imputation is almost worst than doing nothing as it will increase the bias and shrink error estimates.

    • Thank you Jason. I agree that is risky to impute missing values with mean and median when it comes to predictive models. Our goal here is to show how you can do some things. Regarding the `mice` package. It returns the missing values based on probabilistic models which also can be biased.


Leave a Comment

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore


Text Summarization with Transformers

We have provided a walkthrough example of Text Summarization with Gensim. Today, we will provide an example of Text Summarization