字符串作为函数参数r

时间:2014-09-23 07:44:33

标签: r string 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 

是否有任何解决方案可以使用列名作为函数参数?

2 个答案:

答案 0 :(得分:6)

如果你想创建一个接受字符串“B”作为参数的函数(如你问题的标题)

foo_string <- function (x) {
         eval(substitute(df %>% filter(xx < 1),list(xx=as.name(x))))
}
foo_string("B")

如果你想创建一个接受捕获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)

您可以在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)

您可以在此vignette

中找到更多信息

答案 1 :(得分:1)

我记得@Richard Scriven回答了一个类似的问题。我想你需要写这样的东西。

foo <- function(x,...)filter(x,...) 

@Richard Scriven提到的是你需要在这里使用...。如果你输入?dplyr,你将能够找到:filter(.data, ...)我认为你用x或其他东西替换.data。如果你想在你的df中选择B中值小于1的行,它就像这样。

foo <- function (x,...) filter(x,...)
foo(df, B < 1)
相关问题