jackahall
jackahall

Reputation: 490

case_when when there are factors

I am trying to combine treatment allocations for patients who completed two different randomisation forms. I can simulate some example data here:

data <- data.frame(id = 1:100,
                   trt_a = factor(c(sample(0:1, 50, TRUE), rep(NA, 50))),
                   trt_b = factor(c(sample(0:1, 50, TRUE), rep(NA, 50))),
                   trt_ab = factor(c(rep(NA, 50), sample(c("a", "b", "ab", "neither"), 50, TRUE))))

Is there any way of creating a new column with the same factor levels as trt_ab? Half the patients had choice of either trt_a or trt_b, and the other half had choice trt_ab. I want to use some sort of case_when statement to generate a new column with the actual treatment choices:

data %>%
  mutate(trt = case_when(trt_a == 0 & trt_b == 0 ~ "neither",
                         trt_a == 1 & trt_b == 0 ~ "a",
                         trt_a == 0 & trt_b == 1 ~ "b",
                         trt_a == 1 & trt_b == 1 ~ "ab",
                         !is.na(trt_ab) ~ trt_ab))

However, when any of the columns are factors, I get the following error:

Error in `mutate()`:
! Problem while computing `trt = case_when(...)`.
Caused by error in `` names(message) <- `*vtmp*` ``:
! 'names' attribute [1] must be the same length as the vector [0]

Upvotes: 0

Views: 36

Answers (1)

akrun
akrun

Reputation: 887501

data %>%
  mutate(trt = case_when(trt_a == 0 & trt_b == 0 ~ "neither",
                         trt_a == 1 & trt_b == 0 ~ "a",
                         trt_a == 0 & trt_b == 1 ~ "b",
                         trt_a == 1 & trt_b == 1 ~ "ab",
                         !is.na(trt_ab) ~ trt_ab)) %>% head

-output

 id trt_a trt_b trt_ab     trt
1  1     0     0   <NA> neither
2  2     0     0   <NA> neither
3  3     1     1   <NA>      ab
4  4     1     1   <NA>      ab
5  5     0     1   <NA>       b
6  6     1     1   <NA>      ab

Upvotes: 1

Related Questions