jns*_*srs 0 string r function character dplyr
我正在使用dplyr并创建代码来计算用ggplot绘制的新数据.
我想用这段代码创建一个函数.它应该采用dplyr操作的数据框列的名称.但是,尝试使用列名不起作用.请考虑以下最小例子:
df <- data.frame(A = seq(-5, 5, 1), B = seq(0,10,1))
library(dplyr)
foo <- function (x) {
df %>%
filter(x < 1)
}
foo(B)
Error in filter_impl(.data, dots(...), environment()) :
object 'B' not found
Run Code Online (Sandbox Code Playgroud)
是否有任何解决方案可以使用列的名称作为函数参数?
如果你想创建一个接受字符串"B"作为参数的函数(如你问题的标题)
foo_string <- function (x) {
eval(substitute(df %>% filter(xx < 1),list(xx=as.name(x))))
}
foo_string("B")
Run Code Online (Sandbox Code Playgroud)
如果你想创建一个接受捕获B作为参数的函数(如在dplyr中)
foo_nse <- function (x) {
# capture the argument without evaluating it
x <- substitute(x)
eval(substitute(df %>% filter(xx < 1),list(xx=x)))
}
foo_nse(B)
Run Code Online (Sandbox Code Playgroud)
您可以在Advanced R中找到更多信息
dplyr在版本0.3中使事情变得更容易.带有后缀"_"的函数接受字符串或表达式作为参数
foo_string <- function (x) {
# construct the string
string <- paste(x,"< 1")
# use filter_ instead of filter
df %>% filter_(string)
}
foo_string("B")
foo_nse <- function (x) {
# capture the argument without evaluating it
x <- substitute(x)
# construct the expression
expression <- lazyeval::interp(quote(xx < 1), xx = x)
# use filter_ instead of filter
df %>% filter_(expression)
}
foo_nse(B)
Run Code Online (Sandbox Code Playgroud)
您可以在此插图中找到更多信息