I have a df where value
indicates the status of a drug
:
g1 = data.frame (
drug = c('a','a','a','d','d'),
value = c('fda','trial','case','case','pre')
)
drug value
1 a fda
2 a trial
3 a case
4 d case
5 d pre
So for drugs, I want to replace any repeat drug
based on the following order-of-priority for value
:
fda > trial > case > pre
So for example if drug d is "case" as well as "pre", all incidence of d will be reclassify as "case". The final table should look like this.
drug value
1 a fda
2 a fda
3 a fda
4 d case
5 d case
How to do this without having to loop through each drug and figuring out the precedence first then replacing?