Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
435 views
in Technique[技术] by (71.8m points)

r - Dplyr - Mutate dynamically named variables using other dynamically named variables

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...