Grant Waters
Grant Waters

Reputation: 21

Using Case_when

I am attempting to change certain values in a column from for example 10,11,4, and 5 into 1s. The column contains values from 1 to 20. I want all the other values, not the numbers listed above to turn into 0s. I think a case when is a good approach to this problem but I would appreciate any help. I am trying to create dummy variables so if there is a better way let me know.

    RUVDM = RUVDM %>% 
  mutate(Extrac = case_when(
    V015 == "10"~ 1,
    V015 == "11"~1,
    V015 == "4"~ 1,
    V015 == "5"~1,
    TRUE ~ 0)
  )

Upvotes: 2

Views: 620

Answers (1)

akrun
akrun

Reputation: 887871

We can use %in% instead of ==

library(dplyr)
RUVDM %>% 
    mutate(Extrac = case_when(
                  V015 %in% c(10, 11, 4, 5) ~ 1, TRUE ~ 0))

Upvotes: 3

Related Questions