R/create_diag.R

Defines functions create_diag

Documented in create_diag

#' Create a new variable based on pattern.
#'
#' Create a new variable based on pattern in the argument expr
#'
#' @param data: input data
#' @param expr: regular expression describing the pattern of interest
#' @param colvec: indices of variables of interest
#' @param ignore.case logical
#' @param perl logical
#'
#' @return new variable matching the pattern described in the regular expression
#' @export
#'
#' @examples

#'
create_diag <- function(data, expr, colvec, ignore.case = T, perl = T) {
  #expr = regular expressions
  # colvec = vector of the columns of interest (columns with the diagnoses). indices
  # or variable names without quotation marks
  colvec = enquo(colvec)
  # assign '1' if the regular expression matched
  f1 = function(x) as.numeric(grepl(expr, x, ignore.case = ignore.case, perl = perl))
  # any one in the diagnosis field suffices
  f2 = function(x){as.numeric(rowSums(x, na.rm = TRUE) > 0)}

  data %>% select(!!colvec) %>%
    mutate_all(funs(as.character)) %>%
    map_df(f1) %>%
    mutate(new_diag = f2(.)) %>%
    pull(new_diag)
}
injuryepi/injuryepi documentation built on Nov. 13, 2019, 12:13 p.m.