I can't figure out why these two are not behaving in the same way. The case_when one works as I expect it to, while if_else one gives the error:
Error: `true` must be length 1 (length of `condition`), not 2
Run `rlang::last_error()` to see where the error occurred.
The example code:
tb <-
tibble(DC = c(1, 1, 2), ID = c(1, 1, 2), V = c(100, 200, 400)) %>%
group_by(DC, ID)
tb %>%
mutate(V = if_else(sum(V) == 300, V / n(), 1))
tb %>%
mutate(V = case_when(sum(V) == 300 ~ V / n(), TRUE ~ 1))
Thanks in advance.