标准 dplyr 动词的示例
NSE 函数应该用于交互式编程。但是,在新软件包中开发新功能时,最好使用 SE 版本。
加载 dplyr 和 lazyeval:
library(dplyr)
library(lazyeval)
过滤
NSE 版本
filter(mtcars, cyl == 8)
filter(mtcars, cyl < 6)
filter(mtcars, cyl < 6 & vs == 1)
SE 版本(在新软件包中编程函数时使用)
filter_(mtcars, .dots = list(~ cyl == 8))
filter_(mtcars, .dots = list(~ cyl < 6))
filter_(mtcars, .dots = list(~ cyl < 6, ~ vs == 1))
总结
NSE 版本
summarise(mtcars, mean(disp))
summarise(mtcars, mean_disp = mean(disp))
SE 版本
summarise_(mtcars, .dots = lazyeval::interp(~ mean(x), x = quote(disp)))
summarise_(mtcars, .dots = setNames(list(lazyeval::interp(~ mean(x), x = quote(disp))), "mean_disp"))
summarise_(mtcars, .dots = list("mean_disp" = lazyeval::interp(~ mean(x), x = quote(disp))))
变异
NSE 版本
mutate(mtcars, displ_l = disp / 61.0237)
SE 版本
mutate_(
.data = mtcars,
.dots = list(
"displ_l" = lazyeval::interp(
~ x / 61.0237, x = quote(disp)
)
)
)