R/tableNA.R

Defines functions tableNA

Documented in tableNA

#' @title 
#' Alternative Table Creation
#' 
#' @description 
#' Count the occurrences of each factor level or value in a vector. 
#' 
#' @param x
#' numeric. An atomic vector or a factor. 
#' 
#' @return 
#' An object of class \code{"tableNA"}, which is the result of 
#' \code{\link{tabulate}()} with three attributes:
#' \itemize{
#'   \item \code{type_of_x}: the result of \code{typeof(x)};
#'   \item \code{is_factor_x}: the result of \code{is.factor(x)};
#'   \item \code{levels}: the result of \code{levels(x)}.
#' }
#' The number of missing values is always reported.
#' 
#' @export
#' 
#' @examples 
#' tableNA(c(1,2,2,1,3))
#' tableNA(c(1,2,2,1,3, NA))
#' 
tableNA <- function(x) {
  
  stopifnot(is.atomic(x))
  if (length(x) == 0L) {
    fx <- factor(x, exclude = NULL, levels = NA)
  } else {
    fx <- factor(x, exclude = NULL)
  }
  tab <- tabulate(fx)
  ns <- levels(fx)
  ns[is.na(ns)] <- "<NA>"
  names(tab) <- ns
  structure(tab, type_of_x = typeof(x), is_factor_x = is.factor(x), 
            levels = levels(x), class = "tableNA")
}

Try the statip package in your browser

Any scripts or data that you put into this service are public.

statip documentation built on Nov. 18, 2019, 1:06 a.m.