如何在r中的函数定义中正确使用dplyr谓词?
我想在我的函数中使用如何在r中的函数定义中正确使用dplyr谓词?,r,dplyr,nse,R,Dplyr,Nse,我想在我的函数中使用dplyr中的filter和summary。如果没有函数,它的工作原理如下: library(dplyr) > Orange %>% + filter(Tree==1) %>% + summarise(age_max = max(age)) age_max 1 1582 df.maker <- function(df, plant, Age){ require(dplyr) dfo <- df %&
dplyr中的filter
和summary
。如果没有函数,它的工作原理如下:
library(dplyr)
> Orange %>%
+ filter(Tree==1) %>%
+ summarise(age_max = max(age))
age_max
1 1582
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
我想在函数中执行相同的操作,但以下操作失败:
## 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
函数定义:
df.maker%
总结(年龄=最大年龄)
返回(dfo)
}
##使用:
>df.maker(橙色、树木、年龄)
使用调试重新运行
as.lazy_点(列表(…)中出错:找不到对象“树”
我知道以前也有人问过类似的问题。我还浏览了一些相关链接,如和。但是我不能完全理解NSE和SE的概念。我尝试了以下几点:
library(dplyr)
> Orange %>%
+ filter(Tree==1) %>%
+ summarise(age_max = max(age))
age_max
1 1582
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
df.maker%
总结u(年龄u max=~max(年龄))
返回(dfo)
}
但是得到同样的错误。请帮我了解发生了什么事。我怎样才能正确地创建我的函数?谢谢
编辑:我还尝试了以下方法:
library(dplyr)
> Orange %>%
+ filter(Tree==1) %>%
+ summarise(age_max = max(age))
age_max
1 1582
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
df.maker%
总结(年龄)max=lazyeval::interp(~max(x),
x=as.name(年龄)))
返回(dfo)
}
>df.maker(橙色、树木、年龄)
as.name(Age)中出错:找不到对象“Age”
提供字符参数并将用作.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')
或者使用替换
捕获参数:
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)
回答你的问题了吗?@Axeman,我试过了,如我的编辑所示。但它仍然不起作用。我认为这与环境有关。非常感谢!我不知道
substitute()
。