Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.

library(tidyverse)

df<-tibble(id=seq(1,10), ColumnA=c(10,9,8,7,NA,NA,20,15,12,NA),
ColumnB=factor(c("A","B","A","A","","B","A","B","","A")),
ColumnC=factor(c("","BB","CC","BB","BB","CC","AA","BB","","AA")),
ColumnD=c(NA,20,18,22,18,17,19,NA,17,23)
)

df

# A tibble: 10 x 5
id ColumnA ColumnB ColumnC ColumnD

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){
v=v[nchar(as.character(v))>0]
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), is.na(!!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

> df
# A tibble: 10 x 5
id ColumnA ColumnB ColumnC ColumnD

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!