tidyverse / dplyr

dplyr: A grammar of data manipulation
https://dplyr.tidyverse.org/
Other
4.75k stars 2.12k forks source link

[improvement] write examples using SE functions #2408

Closed pachevalier closed 7 years ago

pachevalier commented 7 years ago

Dplyr NSE (non standard evaluation) functions are easy to use. SE functions however are much more difficult and there are only few examples in the documentation.

The NSE vignette provides nice examples for summarise_ but not for the other verbs such as group_by_(), filter_(), etc.

The documentation for select() provides some examples.

I would suggest to add SE examples for filter_() :

filter(mtcars, cyl == 8)
filter_(mtcars, .dots = list(~ cyl == 8))

filter(mtcars, cyl < 6)
filter_(mtcars, .dots = list(~ cyl < 6))

# Multiple criteria
filter(mtcars, cyl < 6 & vs == 1)
filter_(mtcars, .dots = list(~ cyl < 6, ~ vs == 1))

filter(mtcars, cyl < 6 | vs == 1)
filter_(mtcars, .dots = list(~ cyl < 6 | vs == 1))

I would suggest the following examples for summarise_ and groupby :

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

summarise(group_by(mtcars, cyl), mean(disp))
summarise_(group_by_(mtcars, ~ cyl), .dots = list("mean_disp" = lazyeval::interp(~ mean(x), x = quote(disp))))

summarise(group_by(mtcars, cyl), m = mean(disp), sd = sd(disp))
summarise_(group_by_(mtcars, ~ cyl), .dots = list("m" = lazyeval::interp(~ mean(x), x = quote(disp)), "sd" = lazyeval::interp(~ sd(x), x = quote(disp))))

For mutate_, I would add :

mutate(mtcars, displ_l = disp / 61.0237)
mutate_(.data = mtcars, .dots = list("displ_l" = lazyeval::interp(~ x / 61.0237, x = quote(disp))))
krlmlr commented 7 years ago

Thanks. This feels related to #2386 (shelved for now), we should wait a bit to see if alternative APIs become available before updating documentation.

hadley commented 7 years ago

Yeah, this is all going to change as part of the switch to tidyeval. We'll definitely improve the docs.