R/map_var.R

#' Mapper to add extra details
#'
#' @param x vector to be changed
#' @param from regular expression to change
#' @param to word to change to
#'
#' @return updated vector
#' @export
#'
#' @examples
#' map_biomarker  <- readxl::read_excel("data-raw/2018_12_12-mappers.xlsx", sheet = 1)
#' biomarkers  <- readxl::read_excel("data-raw/2018_12_10-biomarkers.xlsx")
#' biomarkers %>%
#'   mutate(
#'     System = map_var(
#'       BIOMARKER,
#'       from = map_biomarker$from,
#'       to = map_biomarker$System
#'     ))
map_var <- function(x, from, to){
  stopifnot(length(from) == length(to))
  # Create output file
  y  <- character(length(x))
  # For each entry, replace from with to
  for(i in 1:length(x)){
    index  <- which(from == x[i])
    if(length(index) > 0){
      y[i]  <- to[index]
    }
  }
  return(y)
}
jonotuke/painBiomarkR documentation built on May 13, 2019, 3:01 a.m.