dplyr casewhen the multiple substring conditions are met after the first one

With the extra details, it looks like the cause might have been substr. The input to this function is:

substr(x, start, stop)

From your code, it looks like you were using the third argument as the number of characters to return, rather than the character position to stop on (documentation here).

The following produces your desired output for me:


data = data.frame(AIRLINE_RE = c("NNNNNNNNNNNYNNN",

data %>% mutate(PLANE_TYPE = case_when(
  substr(AIRLINE_RE,12,12) == "Y" ~ "HLT",
  substr(AIRLINE_RE,4,4) == "Y" ~ "ALA",
  substr(AIRLINE_RE,6,6) == "Y" ~ "WTC",
  TRUE ~ "Unknown"))

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top