I'm trying to add a new column to a dataframe, and I'm not sure how to fit in a conditional statement within the mutate() command. I'm getting error messages.
mutate()
What I have
Code C-1 C-2 C-3
What I want
Code Description C-1 Neighborhood Commercial C-2 General Commercial C-3 Central Business type or paste code here
Hi,
for something like this you can use case_when
library(tidyverse) df <- df %>% mutate( description = case_when(Code == "C-1" ~ "Neighborhood Commercial", Code == "C-2" ~ "General Commercial", Code == "C-3" ~ "Central Business", TRUE ~ "other") )
mutate(description = case_when(Code == "C-1" ~ "Neighborhood Commerical", "C-2" ~ General Commercial", "C-3" ~ "central Business")