Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dplyr - Mutate dynamically named variables using other dynamically named variables

Tags:

r

dplyr

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.

like image 220
ArzaanK Avatar asked Nov 22 '17 17:11

ArzaanK


1 Answers

Here, we don't need the enquo/quo_name for 'year' as we are passing a numeric value. The output of paste will be character class, using sym from rlang (as @joran mentioned) this can be converted to symbol and evaluated with !!. Make sure to add braces around the '!! calc1_header' and '!! calc2_header' to evaluate the specific object

my_fun <- function(df, year) {

  total_header <- paste("total", year, sep = "_")
  calc1_header <- rlang::sym(paste("value1", year, sep = "_"))
  calc2_header <- rlang::sym(paste("value2", year, sep = "_"))

 df %>%
       mutate(!!total_header := multiplier * (!!calc1_header) + (!!calc2_header))



}

my_fun(df1, 2016)
#   ID multiplier value1_2015 value2_2015 value1_2016 value2_2016 total_2016
#1  1        0.5           2           3           1           4        4.5
#2  2        1.0           2           4           4           5        9.0
like image 89
akrun Avatar answered Sep 21 '22 06:09

akrun