I am trying to create new column with grepl(partial match) and ifelse using mutate. I have list of drug names with unit and I want to create new column with these units. Here is my drug name samples,
Description
ZOLEDRONIC ACID INF 4MG/5ML (5ML)
CALCICHEW D3 FORTE CHEWABLE TABS
DAYCASE DRUGS
AKYNZEO (NETUPITANT/PALONOSETRON) HARD CAPS 300MG/0.5MG
CYCLOPHOSPHAMIDE INF 1MG/ML
EPIRUBICIN INJ 1MG/ML
DAYCASE DRUGS
DAYCASE DRUGS
ALOXI (PALONOSETRON) INJ 250MCG
PACLITAXEL INF 1MG/ML
DEXAMETHASONE VIAL 6.6MG/2ML - USE PSD742
PACLITAXEL INF 1MG/ML
RAMUCIRUMAB INF 0.1MG - 100MG
Here is my tried code,
units <- c("TABS","INF","SYR","CAPS","INJ","VIAL","SOL","POWDER","GEL","CREAM","LOTION","AMP"," SINGLEJECT","PFS")
drug_name <- drug_name %>% mutate(unit = grepl(paste(units,collapse = "|"),Description))
current output is like this,
Description unit
ZOLEDRONIC ACID INF 4MG/5ML (5ML) TRUE
CALCICHEW D3 FORTE CHEWABLE TABS TRUE
DAYCASE DRUGS FALSE
AKYNZEO (NETUPITANT/PALONOSETRON) CAPS TRUE
CYCLOPHOSPHAMIDE INF 1MG/ML TRUE
EPIRUBICIN INJ 1MG/ML TRUE
DAYCASE DRUGS FALSE
DAYCASE DRUGS FALSE
I dont want unit in TRUE/FALSE, instead it should specify the exact unit, Like this,
ZOLEDRONIC ACID INF 4MG/5ML (5ML) INF
CALCICHEW D3 FORTE CHEWABLE TABS TABS
I am trying to recode this one but dont know how to do this. Also, I need to extract metrics in drug name (4MG/5ML) into a separate column. I can do this in another query but need to do it single query with unit. Not all the drugs have this format some of them like this--> 01.MG-100MG, 250MCG.
ZOLEDRONIC ACID INF 4MG/5ML (5ML) INF 4MG/5ML
CALCICHEW D3 FORTE CHEWABLE TABS TABS NA
CYCLOPHOSPHAMIDE INF 1MG/ML INF 1MG/ML
ALOXI (PALONOSETRON) INJ 250MCG INF 250MCG
Can anyone help me to solve this?
drug_name <- drug_name %>% mutate(unit = sub(paste0("^(?:.*(", paste(units,collapse = "|"), ").*|.*)$"), "\\1", Description))
– Wiktor Stribiżew