R case_when and %in%

You could actually just use ifelse here:

pres2016$rust_belt <- ifelse(pres2016$state %in% c("OH", "MI", "PA", "WI","IN", "IL"), 1, 0)

If you really want to use case_when here, then just provide a default option using TRUE ~ ...:

pres2016 <- pres2016 %>%
    mutate(rust_belt = case_when(state %in% c("OH", "MI", "PA", "WI","IN", "IL") ~ 1,
                                 TRUE ~ 0))

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top