Analogous function for summarise and summarise_all in dplyr.

summarise_dt(data, ..., by, keyby, fun = NULL)

summarize_dt(data, ..., by, keyby, fun = NULL)

summarise_all_dt(data, fun, by, keyby)

summarize_all_dt(data, fun, by, keyby)

Arguments

data

data.frame

...

List of variables or name-value pairs of summary/modifications functions.

by

unquoted name of grouping variable of list of unquoted names of grouping variables. For details see data.table

keyby

Same as by, but with an additional setkey() run on the by columns of the result, for convenience. It is common practice to use 'keyby=' routinely when you wish the result to be sorted. For details see data.table.

fun

function which will be applied to all variables in dt_summarize and dt_summarize_all.

Value

data.table

See also

Examples

iris %>% summarise_dt(avg = mean(Sepal.Length))
#> avg #> 1: 5.843333
iris %>% summarise_dt(avg = mean(Sepal.Length),by = Species)
#> Species avg #> 1: setosa 5.006 #> 2: versicolor 5.936 #> 3: virginica 6.588
mtcars %>% summarise_dt(avg = mean(hp),by = .(cyl,vs))
#> cyl vs avg #> 1: 6 0 131.6667 #> 2: 4 1 81.8000 #> 3: 6 1 115.2500 #> 4: 8 0 209.2143 #> 5: 4 0 91.0000
# the data.table way mtcars %>% summarise_dt(cyl_n = .N, by = .(cyl, vs)) # `.`` is short for list
#> cyl vs cyl_n #> 1: 6 0 3 #> 2: 4 1 10 #> 3: 6 1 4 #> 4: 8 0 14 #> 5: 4 0 1
iris %>%summarise_all_dt(fun = mean,by = Species)
#> Species Sepal.Length Sepal.Width Petal.Length Petal.Width #> 1: setosa 5.006 3.428 1.462 0.246 #> 2: versicolor 5.936 2.770 4.260 1.326 #> 3: virginica 6.588 2.974 5.552 2.026