標準 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)
            )
         )
)