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
Something like the following?
library(tidyverse)
z <- tibble(a = c(40, 30, NA),
b = c(NA, 20, 10))
z %>%
mutate(result = case_when(
!is.na(a) ~ str_c(a, "a", sep = " "),
is.na(a) & !is.na(b) ~ str_c(b, "b", sep = " "))) %>%
separate(result, into=c("result", "result_logic"), convert = T)
#> # A tibble: 3 × 4
#> a b result result_logic
#> <dbl> <dbl> <int> <chr>
#> 1 40 NA 40 a
#> 2 30 20 30 a
#> 3 NA 10 10 b
Here is an alternative approach dplyr only:
library(dplyr)
z %>%
mutate(result = case_when(
!is.na(a) ~ a,
is.na(a) & !is.na(b) ~ b),
across(-result, ~case_when(
!is.na(.) ~ cur_column()), .names = 'new_{col}'),
result_logic = coalesce(new_a, new_b), .keep="unused")
a b result result_logic
<dbl> <dbl> <dbl> <chr>
1 40 NA 40 a
2 30 20 30 a
3 NA 10 10 b
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With