R/identify_variables.r

#' @title identify_variables
#'
#' @description identifies the type of variables in a \code{\link[base]{data.frame}}
#'
#' @export
#'
#' @param data a \code{\link[base]{data.frame}} to examine
#' @return a \code{\link[tibble]{tibble}} containing the following components:
#' \describe{
#'   \item{name}{the name of the column}
#'   \item{class}{the \code{\link[base]{class}} of the column}
#'   \item{type}{the type (categorical/continuous) of the column}
#' }
#'
#' @author Mark Newman, \email{mark@trinetteandmark.com}
#' @keywords utilities
#' @family utilities
#'
#' @examples
#'   \dontshow{
#'     library(magrittr)
#'     library(mndredge) }
#'   data.frame(
#'     a = 1:10,
#'     b = rep("a", 10),
#'     c = rep("b", 10) %>% factor(),
#'     stringsAsFactors = FALSE) %>%
#'     identify_variables()
#'
identify_variables <- function(data) {
  
  stopifnot(data %>% is.data.frame())
  
  is_categorical <- function(x) { x == "factor" }
  is_continuous <- function(x) { x == "numeric" | x == "integer" }
  class_to_type <- function(class) {
    ifelse(
      is_categorical(class),
      "categorical",
      ifelse(
        is_continuous(class),
        "continuous",
        NA))}
  
  data %>%
    colnames() %>%
    as_tibble() %>%
    set_colnames(c("name")) %>%
    add_column(class = data %>% sapply(class)) %>%
    add_column(., type = class_to_type(.$class))
}
markanewman/mndredge documentation built on May 9, 2019, 5:52 a.m.