R/cm_to.R

#' Centimeter to other units converter
#'
#' This function allows you to convert centimeters to other distance units
#'
#' @param
#' number - number to convert
#' @param
#' to - add unit to convert to, available units 'km', 'm', 'ft', 'in', 'mile'
#'
#' @return Function returns converted values
#' @export
#' @examples
#' cm_to(100, 'm')
#'
#' height = c(170, 184, 192)
#' cm_to(height, 'ft')
#'
cm_to <- function(number, to) {

    x = dplyr::case_when(to == "km" ~ "0.0001",
                         to == "m" ~ "0.01",
                         to == "ft" ~ "0.032808399",
                         to == "in" ~ "0.393700787",
                         to == "mile" ~ "6.21371192e-06",
                         TRUE ~ as.character(NA))

    if (is.na(x)) warning("incorrect measure unit")

    return(number * as.numeric(x))
}
bluemetrica-training/converter documentation built on May 9, 2019, 4:22 a.m.