I am using case_when()
from dplyr
to create the following column, result
.
z <- tibble(a = c(40, 30, NA),
b = c(NA, 20, 10))
z %>%
mutate(result = case_when(
!is.na(a) ~ a,
is.na(a) & !is.na(b) ~ b
)
)
The above gives the following:
a b result
<dbl> <dbl> <dbl>
1 40 NA 40
2 30 20 30
3 NA 10 10
However, I would like to simultaneously create another column, result_logic
, which displays where the value in result
is pulling from (either a or b). The output would look like this.
a b result result_logic
<dbl> <dbl> <dbl> <chr>
1 40 NA 40 a
2 30 20 30 a
3 NA 10 10 b
Is there any way to capture this logic evaluated in case_when()
?
Thanks