R/makeBinVar.R

#' makeBinVar
#'
#' This function will create a single binary variables to indicate the presence or absence of a
#' each element of a user-defined vector of n strings, where the string of interest may appear in
#' one or more data.frame columns.
#' @param x A character vector of the codes of interest
#' @param y Either a character vector reflecting the names of df columns to look through for x, or a numeric vector of column indices
#' @param df A dataframe containing individual claims-level data.
#' "newBinVars" will be applied as default unless another string is given.




makeBinVar <- function(x,y,df) {
  #x is the code of interest. a single code for now
  #y is a character vector reflecting the names of df columns to look through for x
  #df is the data frame of interest

  #first job, get the column indices for each element of y
  colNums <- which(names(df) %in%  y)
  #second job, create the new variable as a list
  newBinVar <- list(ifelse(rowSums(df[, colNums]== x, na.rm=TRUE) >0,1,0))
  #third job, name the variable something sensible
  names(newBinVar) <-  paste0("b_", x)
  #fourth job, name the
  assign(paste0("b_", x), newBinVar, pos = 1)
}
etesdahl/claimWranglR documentation built on May 16, 2019, 8:55 a.m.