问题描述
我想在 dplyr
的函数中使用变量名作为字符串。请参见下面的示例:
I want to use variable names as strings in functions of dplyr
. See the example below:
df <- data.frame(
color = c("blue", "black", "blue", "blue", "black"),
value = 1:5)
filter(df, color == "blue")
它工作得很好,但我想通过字符串引用 color
这个:
It works perfectly, but I would like to refer to color
by string, something like this:
var <- "color"
filter(df, this_probably_should_be_a_function(var) == "blue").
我会很高兴,以任何方式做到这一点,超级快乐,要阅读 dplyr
语法。
I would be happy, to do this by any means and super-happy to make use of easy-to-read dplyr
syntax.
推荐答案
自 dplyr 0.3
每个 dplyr
函数使用非标准评估(NSE,请参阅和)有一个标准评估(SE)双胞胎以下划线结尾。这些可以用于传递变量。对于过滤器
,它将是 filter _
。使用 filter _
可以将逻辑条件作为字符串传递。
As of dplyr 0.3
every dplyr
function using non standard evaluation (NSE, see release post and vignette) has a standard evaluation (SE) twin ending in an underscore. These can be used for passing variables. For filter
it will be filter_
. Using filter_
you may pass the logical condition as a string.
filter_(df, "color=='blue'")
# color value
# 1 blue 1
# 2 blue 3
# 3 blue 4
使用逻辑条件来处理字符串当然是直接的
Construing the string with the logical condition is of course straighforward
l <- paste(var, "==", "'blue'")
filter_(df, l)
这篇关于在`dplyr`的函数中使用变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!