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

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

乘数* value1_year + value2_year ,我称 my_fun (df,2016)我应该收到

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)

}

当我尝试此操作,我在mutate_impl(.data,点)中收到以下错误:
评估错误:二进制运算符的非数字参数。

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 替换表达式,不会出错,产生正确的列名,但是列中的value是字符串 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 我们传递数字值时的年。 paste 的输出将是 character 类,使用 sym rlang (如@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天全站免登陆