BB1234
BB1234

Reputation: 15

Combining different dummy variables into a single categorical variable based on conditions (mutually exclusive categories)?

I am new to R, and trying to create a new variable based on three dummy variables into a single categorical variable with 4 levels (0,1,2,3) which should contain mutually exclusive categories.

There are some specifications to the output I want. If g_kom = 0 then the new variable should have the value of 0. If g_kom = 1 then the new variable should have the value of 1. If g_kom = 1 and v_kom = 1 then the new variable should have the value of 2. If g_kom = 1 and v_kom is 1 and a_kom is 1 then the new variable should have the value of 3. Also the levels should be ordered from 0-3.

The challenge is that the dummy-variables I've created is not from a single categorical variable, so that the groups are not mutually exclusive across the different dummy-variables.

I've tried the following code, but all I end up with then is a dichotomous variable, and not one with 4 levels. I guess its because the groups is not mutually exclusive across the different dummy variables, but this is the part I need help with.

mydata1 <- mydata %>% mutate(kat_kom1 = case_when((g_kom == 0) ~ 0,
                                (g_kom == 1) ~ 1,
                                (g_kom == 1 & v_kom == 1) ~ 2,
                                (g_kom == 1 & v_kom == 1 & a_kom == 1) ~ 3))

As I stated previously, I am a beginner in R, so hopefully some of you with more experience may be able to provide me with an answer to my question. Thanks.

Upvotes: 0

Views: 831

Answers (2)

akrun
akrun

Reputation: 886938

We can use a key/value dataset and do a join

library(dplyr)
keydat <- data.frame(g_kom = 1, v_kom = c(0, 0, 1, 1), 
                       a_kom = c(0, 1, 0, 1), kat_kom1 = c(1, 4, 2, 3))

left_join(mydata, keydat) %>%
     mutate(kat_kom1 = replace(kat_kom1, g_kom == 0, 0))

Upvotes: 1

Rui Barradas
Rui Barradas

Reputation: 76402

Try to also include v_kom and a_kom in the several cases, not just the next ones. Untested, since there is no data example.

mydata1 <- mydata %>% 
  mutate(kat_kom1 = case_when(
    (g_kom == 0) ~ 0L,
    (g_kom == 1 & v_kom == 0 & a_kom == 0) ~ 1L,
    (g_kom == 1 & v_kom == 1 & a_kom == 0) ~ 2L,
    (g_kom == 1 & v_kom == 1 & a_kom == 1) ~ 3L,
    TRUE ~ NA_integer_)
  )

Upvotes: 1

Related Questions