How to use custom function to create new binary variables within existing dataframe?

后端 未结 1 896
名媛妹妹
名媛妹妹 2021-01-27 15:09

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

相关标签:
1条回答
  • 2021-01-27 15:41

    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")
    
    0 讨论(0)
提交回复
热议问题