I am trying to create a new column with a dynamically created name and populate the field with an expression involving other dynamically created variables. For instance, consider the below data frame.
ID multiplier value1_2015 value2_2015 value1_2016 value2_2016
1 0.5 2 3 1 4
2 1.0 2 4 5 1
I want to write a function which is given the data frame, and a year and then evaluates an expression for only the corresponding year variables, and stores the result in a column called total_year
, where year
is the value given to the function. For instance, if the expression was
multiplier * value1_year + value2_year
and I called my_fun(df, 2016)
I should receive
ID multiplier value1_2015 value2_2015 value1_2016 value2_2016 total_2016
1 0.5 2 3 1 4 4.5
2 1.0 2 4 4 5 9
Here is what I have
my_fun <- function(df, year) {
year <- enquo(year)
total_header <- paste("total", quo_name(year), sep = "_")
calc1_header <- paste("value1", quo_name(year), sep = "_")
calc2_header <- paste("value2", quo_name(year), sep = "_")
calc1_header <- enquo(calc1_header)
calc2_header <- enquo(calc2_header)
ret_table <- df %>%
mutate(!!total_header := multiplier * !!calc1_header + !!calc2_header)
return(ret_table)
}
When I try this I get the following Error in mutate_impl(.data, dots) :
Evaluation error: non-numeric argument to binary operator.
Replacing the expression with something like just !!total_header := !!calc1_header
runs with no error, produces the correct column name, but the values in the column are the string "value1_2016", not the respective values from the column named value1_2016
.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…