Search code examples
rdplyrrecode

Is there a way to create a new column based on the values of another one using dplyr in R?


I have been using base R but I want to use dplyr. This is what I have been doing:

data$newvariable <- 0
data$newvariable[data$oldvariable=="happy"] <- "good"
data$newvariable[data$oldvariable=="unhappy"] <- "bad"
data$newvariable[data$oldvariable=="depressed"] <- "super_bad"

Solution

  • In dplyr, we can use case_when to assign new values to newvariable based on oldvariable.

    library(dplyr)
    
    data = data.frame(
      oldvariable = c("happy", "unhappy", "depressed")
    )
    
    data %>%
      mutate(newvariable = case_when(
        oldvariable == "happy" ~ "good",
        oldvariable == "unhappy" ~ "bad",
        oldvariable == "depressed" ~ "super_bad"
      ))
    #>   oldvariable newvariable
    #> 1       happy        good
    #> 2     unhappy         bad
    #> 3   depressed   super_bad