I\'m trying to create a custom function that generates new binary variables in an existing dataframe. The idea is to be able to feed the function with the diagnosis description
If you are intending to only work with one diagnosis at a time, this will work. I took the liberty of renaming arguments to be a little easier to work with in the code.
diagnosis_func <- function(data, target_col, icd, new_col){
pattern <- sprintf("^(%s)",
paste0(icd, collapse = "|"))
data[[new_col]] <- grepl(pattern = pattern,
x = data[[target_col]]) + 0L
data
}
diagnosis_func(patient_db, "diag_1", "2851", "Anemia")
# Multiple codes for a single diagnosis
diagnosis_func(patient_db, "diag_1", c("8661", "8651"), "Dx")
If you want to spruce it up a little to prevent inadvertent mistakes, you can install the checkmate
package and use this version. This will
diagnosis_func <- function(data, target_col, icd, new_col){
coll <- checkmate::makeAssertCollection()
checkmate::assert_class(x = data,
classes = "data.frame",
add = coll)
checkmate::assert_character(x = target_col,
len = 1,
add = coll)
checkmate::assert_character(x = icd,
add = coll)
checkmate::assert_character(x = new_col,
len = 1,
add = coll)
checkmate::reportAssertions(coll)
pattern <- sprintf("^(%s)",
paste0(icd, collapse = "|"))
data[[new_col]] <- grepl(pattern = pattern,
x = data[[target_col]]) + 0L
data
}
diagnosis_func(patient_db, "diag_1", "2851", "Anemia")