R/util_table_of_vct.R

Defines functions util_table_of_vct

Documented in util_table_of_vct

#' Tabulate a vector
#'
#' does the same as `as.data.frame(table(x))` but
#' guarantees a data frame with two columns is returned
#'
#' @param Var1 vector to ta tabulate
#'
#' @return a data frame with columns `Var1` and `Freq`
util_table_of_vct <- function(Var1) {
  util_expect_scalar(Var1,
                     allow_more_than_one = TRUE,
                     allow_na = TRUE,
                     allow_null = TRUE)
  r <- as.data.frame(table(Var1))
  if (!identical(colnames(r), c("Var1", "Freq"))) {
    r <- data.frame(Var1 = integer(0), Freq = integer(0))
  }
  r
}

Try the dataquieR package in your browser

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

dataquieR documentation built on July 26, 2023, 6:10 p.m.