如何将列名作为参数传递给dplyr中的函数? [英] How to pass column name as parameter to function in dplyr?

查看:159
本文介绍了如何将列名作为参数传递给dplyr中的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要像这里但是使用dplyr和另外一列。

I want to do the same as here but with dplyr and one more column.

我想通过字符串变量选择列,但在顶部我也想要正常选择第二列。
我需要这个,因为我有一个功能,通过给定的参数选择几列。

I want to selecting a column via a string variable, but on top I also want to select a second column normally. I need this because I have a function which selects a couple of columns by a given parameters.

我有以下代码作为一个例子:

I have the following code as an example:

library(dplyr)
data(cars)

x <- "speed"
cars %>% select_(x, dist)


推荐答案

您可以使用 dist() dist

x <- "speed"
cars %>% select_(x, quote(dist)) %>% head
#   speed dist
# 1     4    2
# 2     4   10
# 3     7    4
# 4     7   22
# 5     8   16
# 6     9   10

这篇关于如何将列名作为参数传递给dplyr中的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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