在mutate()中为列函数使用变量 [英] Using variables for column functions in mutate()

查看:93
本文介绍了在mutate()中为列函数使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在dplyr字符串中使用变量代替列名?举例来说,我想向虹膜数据集中添加一列,称为sum,即Sepal.Length和Sepal.Width的总和。简而言之,我想要下面的代码的工作版本。

How can I use variables in place of column names in dplyr strings? As an example say I want to add a column to the iris dataset called sum that is the sum of Sepal.Length and Sepal.Width. In short I want a working version of the below code.

x = "Sepal.Length"
y = "Sepal.Width"
head(iris%>% mutate(sum = x+y))



<当前,运行代码将输出评估错误:二进制运算符的非数字参数,因为R将x和y评估为字符向量。如何改为让R将x和y评估为数据框的列名?我知道答案是使用某种形式的延迟评估,但是我在弄清楚如何配置它时遇到了麻烦。

Currently, running the code outputs "Evaluation error: non-numeric argument to binary operator" as R evaluates x and y as character vectors. How do I instead get R to evaluate x and y as column names of the dataframe? I know that the answer is to use some form of lazy evaluation, but I'm having trouble figuring out exactly how to configure it.

请注意,建议的重复项: dplyr-变异:使用动态变量名称不能解决此问题。重复项回答了以下问题:

Note that the proposed duplicate: dplyr - mutate: use dynamic variable names does not address this issue. The duplicate answers this question:

不是我的问题:我该怎么办:

var = "sum"
head(iris %>% mutate(var = Sepal.Length + Sepal.Width))


推荐答案

我认为推荐的方式是使用 sym

I think that recommended way is using sym:

iris %>% mutate(sum = !!sym(x) + !!sym(y)) %>% head

这篇关于在mutate()中为列函数使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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