在dplyr中总结的函数中使用quasiquotation
我正在尝试编写一个函数,该功能可以将列名作为字符串分配给变量并为其产生汇总输出的字符串,因此
my_function <- function(my_df, columnA,columnB){
summ_df <- my_df %>%
group_by(cyl) %>%
summarise(base_mean = mean(columnA),
contrast_mean = mean(columnB))
return(summ_df)
}
base = "drat"
cont = "wt"
my_function(mtcars,base,cont)
我想要的是,上述功能会返回相同的内容,因为
mtcars %>%
group_by(cyl) %>%
summarise(base_mean = mean(drat),
contrast_mean = mean(wt))
我确定它是否是某种组合enexpr,或ensym和!但是我不断获得NA值
I'm trying to write a function that can take column names as strings assigned to variables and produce a summarized output for them like so
my_function <- function(my_df, columnA,columnB){
summ_df <- my_df %>%
group_by(cyl) %>%
summarise(base_mean = mean(columnA),
contrast_mean = mean(columnB))
return(summ_df)
}
base = "drat"
cont = "wt"
my_function(mtcars,base,cont)
What I would want is that the above function would return the same thing as
mtcars %>%
group_by(cyl) %>%
summarise(base_mean = mean(drat),
contrast_mean = mean(wt))
I'm sure it's some combination of enexpr, or ensym, and !! but i keep getting NA values
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
!! 使用
使用
ENSYM
与Use
ensym
with!!
so that it can take both unquoted and quoted actual arguments-testing