R/miss_count.R

Defines functions miss_count

Documented in miss_count

#' Count missing values in all columns and calculate the percentage
#'
#' @param df A dataframe of type data.frame
#' @param ascending A boolean value to decide whether the df is sorted ascending or decending
#' @return A dataframe with thee columns: column,counts and percentage
#' @export
#'
#' @examples
#' mydf <- data.frame(
#'   name = c("Amy","Tony","Jessica"),
#'   age = c(18,21,30),
#'   hobby = c("lab","quiz","swim")
#' )
#' miss_value <- miss_count(mydf,ascending = FALSE)
#' miss_value
miss_count <- function(df,ascending=FALSE) {
    if (!is.data.frame(df)) {
        stop("The input should be a dataframe")
        }
    if (length(df)==0) {
        stop("The input dataframe is empty")
        }
    counts <- sapply(df,function(x)sum(is.na(x)))
    results <- data.frame(counts,percentage=round(counts/length(df),2))
    if (ascending == FALSE){
        results <-results[order(-results$counts),]
    }else{
        results <-results[order(results$counts),]
    }
    return(results)
}
UBC-MDS/slimreda documentation built on Feb. 7, 2022, 9:12 a.m.