I have a data set which contains a list of treatments (Treatment variable) and then another person has categorised these treatments based on their mechanism of action (Mechanism variable). I want to add another mechanism of action category (Hypothermia) and I am struggling to do so.
I have made a small data frame as an example of some of the treatments and their mechanism categories.
Treatment <- c("Hypothermia", "CNS-1102", "Hypocapnia", "Dextrorphan", "Mannitol", "Caffeinol")
Mechanism <- c("Other", "Excitotoxicity", "Blood flow", "Excitotoxicity", "Fluid regulation", "Other")
df <- data.frame(Treatment, Mechanism)
I'm interested in hypothermia so I want to make a new variable (called Mechanism_extra) which is a copy of Mechanism except it classifies "Hypothermia" as its own category instead of classing "Hypothermia" cases under the "Other" category. My actual data set contains ~8000 entries so I can't just do this manually. I have tried to do this with mutate from dplyr and with ifelse, but my output just doesn't work.
df <- mutate(df, Mechanism_extra = ifelse(df$Treatment == "Hypothermia", "Hypothermia", df$Mechanism))
df$Mechanism_extra
With the above code I'm trying to say "make a new variable called Mechanism_extra, look at the drugs in Treatment and if you see Hypothermia then put Hypothermia into the new variable, if it doesn't say Hypothermia then just write down the original mechanism of action". However my output looks like this:
[1] "Hypothermia" "2" "1" "2" "3" "4"
When I want it to look like this:
[1] "Hypothermia" "Excitotoxicity" "Blood flow" "Excitotoxicity" "Fluid regulation" "Other"
Why are there numbers? Where am I going wrong?
Aucun commentaire:
Enregistrer un commentaire