Hi,
Below is a reprex to illustrate the input, desired output, and a failed attempt; hope to get some enlightenment!
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(tibble)
df <-
tibble::tribble(
~a, ~a_b,
1, NA,
2, 4,
3, 5
)
# desired output
df %>%
mutate(c = if_else(is.na(a_b), 0, a))
#> # A tibble: 3 x 3
#> a a_b c
#> <dbl> <dbl> <dbl>
#> 1 1 NA 0
#> 2 2 4 2
#> 3 3 5 3
# failed attempt
df %>%
mutate_at(vars(a),
funs(if_else(is.na(!!sym(str_c(., '_b'))), 0, .)))