如何将列名作为参数传递给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