R/is_type.R

Defines functions is_type_date is_type_num is_type_cat

Documented in is_type_cat is_type_date is_type_num

#'
#' This function checks if a vector x is of type date. Vectors of class Date or POSIXct
#' are considered being of type date.
#' 
#' @param x a vector
#' 
#' @return a logical, TRUE if date, FAlSE otherwise
#' 
is_type_date <- function(x) {
  any(c('Date', 'POSIXct') %in% class(x))
}

#'
#' This function checks if a vector x is of numerical. A vector is considered numerical if
#' it is of type numeric, has more than min_unique unique values (defaults to 15) and is
#' not of class integer64.
#' 
#' @param x a vector
#' @param min_unique the minimal number of unique values for a numeric vector to be
#'   describe as a numeric vector and not as a categorical vector. Defaults to 15.
#'
#' @return a logical, TRUE if numerical, FAlSE otherwise
#' 
is_type_num <- function(x, min_unique = 15) {
  is.numeric(x) & length(unique(x)) > min_unique & class(x) != 'integer64'
}

#'
#' This function checks if a vector x is categorical. A vector is considered categoriacl
#' if it is of type numericwith less than min_unique unique values (defaults to 15) or it
#' is a character or a factor or a logical.
#' 
#' @param x a vector
#' @param min_unique the minimal number of unique values for a numeric vector to be
#'   describe as a numeric vector and not as a categorical vector. Defaults to 15.
#'   
#' @return a logical, TRUE if categorical, FAlSE otherwise
#' 
is_type_cat <- function(x, min_unique = 15) {
  (is.numeric(x) & length(unique(x)) <= min_unique) |
    (is.numeric(x) & class(x) == 'integer64') |
    is.character(x) |
    is.factor(x) |
    is.logical(x)
}
MathieuMarauri/explorer documentation built on Jan. 8, 2020, 6:37 p.m.