#' Impute mean/median in a data frame
#'
#' @param df \code{data.frame}
#'
#' @details
#' Iterates across all variables and imputes the mean for numerical variables. If a variable is (ordered) categorical, a character vector or logical, the mode is imputed. If a variable has multiple modes, the first-appearing mode is selected.
#'
#' @return
#' \code{data.frame}.
#'
#' @examples
#' df <- tibble(a=1:5, b=LETTERS[1:5])
#' missMaker(df, var='b', size=1)
#' missMean(df)
#'
#' @export
missMean <- function(df){
K <- ncol(df)
for(k in 1:K){
miss <- is.na(df[[k]])
if(sum(miss)==0) next
if( is_cat(df[[k]]) ){
med_ <- qmode(df[[k]],na.rm=TRUE)
df[[k]][miss] <- med_
} else {
mu_ <- mean(df[[k]], na.rm=TRUE)
df[[k]][miss] <- mu_
}
}
return(df)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.