R/helpers_normalize_phecodes.R

Defines functions normalize_phecodes

Documented in normalize_phecodes

#' Normalize a phecode vector to padded character
#'
#' Across ME we work with the phecodes that are provided in a million different
#' formats. This takes any format (float, string) and converts it to a 0-padded
#' character.
#'
#' @param codes A vector of phecodes in whatever format your heart desires. E.g. `8.00`, `8`, `0008.0`, ...
#'
#' @return A vector of 0-padded phecodes.
#' @export
#'
#' @examples
#' normalize_phecodes(c(8, 8.1, 9.2))
normalize_phecodes <- function(codes){

  stringr::str_pad(
    sprintf('%3.2f', as.numeric(codes)),
    width = 6,
    side = "left",
    pad = "0"
  )

}
tbilab/meToolkit documentation built on June 23, 2020, 9:55 a.m.