Dplyr - 使用其他动态命名的变量改变动态命名的变量 [英] Dplyr - Mutate dynamically named variables using other dynamically named variables

查看:16
本文介绍了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

我想编写一个函数,该函数给定数据框和年份,然后仅计算相应年份变量的表达式,并将结果存储在名为 total_year 的列中,其中 year 是给函数的值.例如,如果表达式是

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 我打电话给 my_fun(df, 2016) 我应该收到

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

这是我所拥有的

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)

}

当我尝试这个时,我得到以下 Error in mutate_impl(.data, dots) :计算错误:二元运算符的非数字参数.

When I try this I get the following Error in mutate_impl(.data, dots) : Evaluation error: non-numeric argument to binary operator.

将表达式替换为类似 !!total_header := !!calc1_header 运行没有错误,产生正确的列名,但列中的值是字符串value1_2016",而不是来自名为 value1_2016 的列中的相应值.

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.

推荐答案

在这里,我们不需要 enquo/quo_name 来表示 'year',因为我们正在传递一个数值.paste 的输出将是 character 类,使用 rlang 中的 sym(如@joran 提到的)这可以是转换为符号并用 !! 求值.确保在 '!!calc1_header' 和 '!!calc2_header' 来评估特定对象

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

这篇关于Dplyr - 使用其他动态命名的变量改变动态命名的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆