#' 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)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.