I have a script that successfully creates a new column in the existing data frame, using mutate combined with str_detect to indicate whether a drug component is present in the old variable. I would like to turn this script into a function to make it easier to use repetitively. My attempts to create the function have failed.
Here is the script:
Drug_Table_Names <- data.frame(mutate(
Drug_Table_Names,
DRUG_GENERIC_NAME,
Flurbiprofen = str_detect(Drug_Table_Names$DRUG_GENERIC_NAME,"FLURBIPROFEN", negate = FALSE)
))
The script finds Flurbiprofen in the column DRUG_GENERIC_NAME and creates a new column named FLUBIPROFEN which TRUE if Flurbiprofen is present.
My attempt to create a function FlagDrugNames
was written this way:
function(drug_flag, gen_name){
Drug_Table_Names <- data.frame(mutate(
Drug_Table_Names,
DRUG_GENERIC_NAME,
drug_flag = str_detect(Drug_Table_Names$DRUG_GENERIC_NAME,
"gen_name", negate = FALSE)
))
}
Where drug_flag
is the string the function looks for, in the example above is is Flurbiprofen, and gen_name is the name of the column it creates. This function does not work.
I would appreciate any help with the function.