如何在R函数定义中正确使用dplyr动词?

3

我希望在我的函数中使用dplyr库的filtersummarise函数。如果不使用函数,代码如下:

library(dplyr)
> Orange %>% 
+     filter(Tree==1) %>% 
+     summarise(age_max = max(age))
  age_max
1    1582  

我想在一个函数内做相同的事情,但以下尝试失败:
## Function definition:

df.maker <- function(df, plant, Age){

  require(dplyr)

  dfo <- df %>% 
    filter(plant==1) %>% 
    summarise(age_max = max(Age))

  return(dfo)
}

## Use:
> df.maker(Orange, Tree, age)

 Rerun with Debug
 Error in as.lazy_dots(list(...)) : object 'Tree' not found

我知道之前有类似的问题被提出过。我也阅读了一些相关链接,例如 page1page2。但是我无法完全理解NSE和SE的概念。我试着跟随以下步骤:
df.maker <- function(df, plant, Age){

  require(dplyr)

  dfo <- df %>% 
    filter_(plant==1) %>% 
    summarise_(age_max = ~max(Age))

  return(dfo)
} 

但是我得到了相同的错误。请帮助我理解发生了什么。我该如何正确创建我的函数?谢谢!
编辑:
我也尝试了以下方法:
df.maker <- function(df, plant, Age){

  require(dplyr)

  dfo <- df %>% 
    #filter_(plant==1) %>% 
    summarise_(age_max = lazyeval::interp(~max(x),
                                          x = as.name(Age)))

  return(dfo)
}  

> df.maker(Orange, Tree, age)
 Error in as.name(Age) : object 'age' not found 

1
这个回答解决了你的问题吗? - Axeman
@Axeman,我已经尝试过你说的方法了,就像我编辑时展示的那样。但它仍然无法工作。我认为这与环境有关。 - umair durrani
1个回答

4

如果要使用字符参数,请使用 as.name

df.maker1 <- function(d, plant, Age){
  require(dplyr)
  dfo <- d %>% 
    filter_(lazyeval::interp(~x == 1, x = as.name(plant))) %>% 
    summarise_(age_max = lazyeval::interp(~max(x), x = as.name(Age)))
  return(dfo)
}  
df.maker1(Orange, 'Tree', 'age')
  age_max
1    1582

或者使用substitute捕获参数:

df.maker2 <- function(d, plant, Age){
  require(dplyr)
  plant <- substitute(plant)
  Age <- substitute(Age)

  dfo <- d %>% 
    filter_(lazyeval::interp(~x == 1, x = plant)) %>% 
    summarise_(age_max = lazyeval::interp(~max(x), x = Age))
  return(dfo)
}  
df.maker2(Orange, Tree, age)
  age_max
1    1582

1
非常感谢!我不知道substitute() - umair durrani

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接