Allanna Russell
Allanna Russell

Reputation: 15

In R how do I create a variable whose contents are based on what is in other variables?

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?

Upvotes: 1

Views: 55

Answers (1)

william3031
william3031

Reputation: 1708

You can make it a tibble instead of a data.frame using dplyr and that will work.

library(dplyr)

Treatment <- c("Hypothermia", "CNS-1102", "Hypocapnia", "Dextrorphan", "Mannitol", "Caffeinol")
Mechanism <- c("Other", "Excitotoxicity", "Blood flow", "Excitotoxicity", "Fluid regulation", "Other")
df <- tibble(Treatment, Mechanism) # changed this


df %>% 
  mutate(Mechanism_extra = if_else(Treatment == "Hypothermia", "Hypothermia", Mechanism))

Which is this:

# A tibble: 6 x 3
  Treatment   Mechanism        Mechanism_extra 
  <chr>       <chr>            <chr>           
1 Hypothermia Other            Hypothermia     
2 CNS-1102    Excitotoxicity   Excitotoxicity  
3 Hypocapnia  Blood flow       Blood flow      
4 Dextrorphan Excitotoxicity   Excitotoxicity  
5 Mannitol    Fluid regulation Fluid regulation
6 Caffeinol   Other            Other 

Upvotes: 1

Related Questions