This answer indicates how to dynamically assign column names for the variable on the left side of the :=
operator, but I can't get it to work for assignment on the right side of the operator. The following toy example illustrates what I'm trying to accomplish:
library(tidyverse)
df <- tribble(
~geo, ~type, ~a_b_avg, ~bperPerson,
"a", "b", 3, 2,
"a", "b", 3, 4
)
f15 <- function(.data, geo, type) {
df %>%
mutate("{{geo}}_{{type}}_15" := if_else("{{type}}perPerson" <
"{{geo}}_{{type}}_avg",
1,
0))
}
f15(df, "a", "b")
I would like to end up with a new column, a_b_15, which equals 1 in the first row (where bperPerson < a_b_avg) and 0 in the second row (where bperPerson > a_b_avg). Instead, I get the column `"a"_"b"_15`, with both values equal to zero.