Я хочу использовать filter
и summarise
из dplyr
внутри своей функции. Без функции это работает следующим образом:
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
Я знаю, что подобные вопросы уже задавались. Я также просмотрел некоторые соответствующие ссылки, такие как page1 и страница2. Но я не могу полностью понять концепции 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