Summarise group of values into one value for each group. If there is only one group, then only one value would be returned. The summarise function should always return a single value.

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

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

summarise_when(.data, when, ..., by = NULL)

summarize_when(.data, when, ..., by = NULL)

summarise_vars(.data, .cols = NULL, .func, ..., by)

summarize_vars(.data, .cols = NULL, .func, ..., by)

Arguments

.data

data.frame

...

List of variables or name-value pairs of summary/modifications functions for summarise_dt.Additional parameters to be passed to parameter '.func' in summarise_vars.

by

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

when

An object which can be coerced to logical mode

.cols

Columns to be summarised.

.func

Function to be run within each column, should return a value or vectors with same length.

Value

data.table

Details

summarise_vars could complete summarise on specific columns.

See also

Examples

iris %>% summarise_dt(avg = mean(Sepal.Length))
#>         avg
#>       <num>
#> 1: 5.843333
iris %>% summarise_dt(avg = mean(Sepal.Length),by = Species)
#>       Species   avg
#>        <fctr> <num>
#> 1:     setosa 5.006
#> 2: versicolor 5.936
#> 3:  virginica 6.588
mtcars %>% summarise_dt(avg = mean(hp),by = .(cyl,vs))
#>      cyl    vs      avg
#>    <num> <num>    <num>
#> 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
#>    <num> <num> <int>
#> 1:     6     0     3
#> 2:     4     1    10
#> 3:     6     1     4
#> 4:     8     0    14
#> 5:     4     0     1

iris %>% summarise_vars(is.numeric,min)
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width
#>           <num>       <num>        <num>       <num>
#> 1:          4.3           2            1         0.1
iris %>% summarise_vars(-is.factor,min)
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width
#>           <num>       <num>        <num>       <num>
#> 1:          4.3           2            1         0.1
iris %>% summarise_vars(1:4,min)
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width
#>           <num>       <num>        <num>       <num>
#> 1:          4.3           2            1         0.1

iris %>% summarise_vars(is.numeric,min,by ="Species")
#>       Species Sepal.Length Sepal.Width Petal.Length Petal.Width
#>        <fctr>        <num>       <num>        <num>       <num>
#> 1:     setosa          4.3         2.3          1.0         0.1
#> 2: versicolor          4.9         2.0          3.0         1.0
#> 3:  virginica          4.9         2.2          4.5         1.4
mtcars %>% summarise_vars(is.numeric,mean,by = c("vs", "am"))
#>       vs    am      mpg      cyl     disp        hp     drat       wt     qsec
#>    <num> <num>    <num>    <num>    <num>     <num>    <num>    <num>    <num>
#> 1:     0     1 19.75000 6.333333 206.2167 180.83333 3.935000 2.857500 15.79667
#> 2:     1     1 28.37143 4.000000  89.8000  80.57143 4.148571 2.028286 18.70000
#> 3:     1     0 20.74286 5.142857 175.1143 102.14286 3.570000 3.194286 19.96714
#> 4:     0     0 15.05000 8.000000 357.6167 194.16667 3.120833 4.104083 17.14250
#>        gear     carb
#>       <num>    <num>
#> 1: 4.666667 4.666667
#> 2: 4.142857 1.428571
#> 3: 3.571429 2.142857
#> 4: 3.000000 3.083333

# use multiple functions on multiple columns
iris %>%
  summarise_vars(is.numeric,.func = list(mean,sd,median))
#>    Sepal.Length Sepal.Width Petal.Length Petal.Width fun_name
#>           <num>       <num>        <num>       <num>   <char>
#> 1:    5.8433333   3.0573333     3.758000   1.1993333     mean
#> 2:    0.8280661   0.4358663     1.765298   0.7622377       sd
#> 3:    5.8000000   3.0000000     4.350000   1.3000000   median
iris %>%
  summarise_vars(is.numeric,.func = list(mean,sd,median),by = Species)
#>       Species Sepal.Length Sepal.Width Petal.Length Petal.Width fun_name
#>        <fctr>        <num>       <num>        <num>       <num>   <char>
#> 1:     setosa    5.0060000   3.4280000    1.4620000   0.2460000     mean
#> 2: versicolor    5.9360000   2.7700000    4.2600000   1.3260000     mean
#> 3:  virginica    6.5880000   2.9740000    5.5520000   2.0260000     mean
#> 4:     setosa    0.3524897   0.3790644    0.1736640   0.1053856       sd
#> 5: versicolor    0.5161711   0.3137983    0.4699110   0.1977527       sd
#> 6:  virginica    0.6358796   0.3224966    0.5518947   0.2746501       sd
#> 7:     setosa    5.0000000   3.4000000    1.5000000   0.2000000   median
#> 8: versicolor    5.9000000   2.8000000    4.3500000   1.3000000   median
#> 9:  virginica    6.5000000   3.0000000    5.5500000   2.0000000   median