Trouble with conditional statements

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.

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")

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.