
Functions for summarising data and counting observations in data frames.
These are SciViews::R versions of tidyverse functions with standard
evaluation and formula-based non-standard evaluation (ending with underscore
_). They work with data.frame, data.table, and tibbles.
Functions:
summarise_() / summarize_() - Compute summary statistics for groups
reframe_() - Similar to summarise but always returns ungrouped data
count_() - Count observations by group
tally_() - Count total observations (wrapper around count_)
add_count_() - Add count column to data frame
add_tally_() - Add total count column to data frame
summarise_(
.data = (.),
...,
.by = NULL,
.groups = "drop_last",
.keep.group_vars = TRUE,
.cols = NULL
)
summarize_(
.data = (.),
...,
.by = NULL,
.groups = "drop_last",
.keep.group_vars = TRUE,
.cols = NULL
)
reframe_(
.data,
...,
.by = NULL,
.groups = "drop",
.keep.group_vars = TRUE,
.cols = NULL
)
count_(
.data = (.),
...,
wt = NULL,
name = "n",
sort = FALSE,
decreasing = TRUE,
.drop = TRUE,
add = FALSE
)
tally_(.data = (.), wt = NULL, name = "n", sort = FALSE, decreasing = TRUE)
add_count_(
.data = (.),
...,
wt = NULL,
name = "n",
sort = FALSE,
decreasing = TRUE,
.drop = TRUE
)
add_tally_(.data = (.), wt = NULL, name = "n", sort = FALSE, decreasing = TRUE)A data frame (data.frame, data.table, or tibble)
For summarise_() and reframe_(): name-value pairs of summary
functions. Names are column names in the output; values are expressions to
compute. Use formulas for non-standard evaluation (e.g., ~mean_mpg = mean(~mpg)).
For count_() and add_count_(): grouping variables specified as formulas
(e.g., ~cyl, ~gear) or character names. Can include named expressions
to create new grouping variables before counting.
Optional temporary grouping variables for per-group computations.
Provide as formulas (e.g., ~group_col) or character names. Groups are
temporary and not preserved in the output. Cannot be used with grouped
data frames.
Control grouping of the result. Options:
"drop_last" (default) - Drop the last grouping level
"drop" - Remove all grouping
"keep" - Keep all grouping levels
"rowwise" - Not implemented
For reframe_(), only "drop" is allowed.
Logical. If TRUE (default), keep grouping variables
in the result.
Optional character vector of column names to operate on. Currently
only NULL (default) is implemented.
For count_(), tally_(), add_count_(), and add_tally_():
frequency weights. Can be NULL (default, counts rows), a numeric vector,
a column name as character, or a formula (e.g., ~weight_col).
Character string specifying the name of the count column created
in the output. Default is "n".
Logical. If TRUE, sort the result by the count column in
decreasing order (or as specified by decreasing). Default is FALSE.
Logical. If TRUE (default), sort counts in decreasing
order when sort = TRUE.
Logical. If TRUE (default), drop unused factor levels. Note:
.drop = FALSE is not yet implemented in count_().
Logical. If TRUE, add the count column to the original data
frame instead of returning a summary. Default is FALSE.
summarise_() returns a data frame with one row per group (or one row if
ungrouped), containing the summary statistics. Grouping depends on .groups.
reframe_() returns an ungrouped data frame (can have any number of rows
per group).
count_() returns a data frame with one row per unique combination of
grouping variables, plus a count column.
tally_() returns a data frame with one row per group showing the count.
add_count_() returns the original data with an additional count column.
add_tally_() returns the original data with an additional count column.
The summarise_() function does not support n() as does dplyr::summarise().
You can use svBase::fn() instead, but then you must give a variable name as
argument. The svBase::fn() alternative can also be used in dplyr::summarise()
for homogeneous syntax between the two.
library(svTidy)
data(mtcars)
# Basic summarise - single summary statistic
mtcars |> summarise_(mean_mpg = ~mean(mpg))
#> mean_mpg
#> 1 20.09062
# Multiple summary statistics
mtcars |>
summarise_(
mean_mpg = ~mean(mpg),
sd_mpg = ~sd(mpg),
max_hp = ~max(hp)
)
#> mean_mpg sd_mpg max_hp
#> 1 20.09062 6.026948 335
# Summarise by groups
mtcars |>
group_by_(~cyl) |>
summarise_(
mean_mpg = ~mean(mpg),
mean_hp = ~mean(hp)
)
#> cyl mean_mpg mean_hp
#> 1 4 26.66364 82.63636
#> 2 6 19.74286 122.28571
#> 3 8 15.10000 209.21429
# Use .by for temporary grouping
mtcars |>
summarise_(
mean_mpg = ~mean(mpg),
count = ~length(mpg),
.by = 'cyl'
)
#> cyl mean_mpg count
#> 1 6 19.74286 7
#> 2 4 26.66364 11
#> 3 8 15.10000 14
# Multiple grouping variables with .by
mtcars |>
summarise_(
mean_mpg = ~mean(mpg),
.by = c('cyl', 'gear')
)
#> cyl gear mean_mpg
#> 1 6 4 19.750
#> 2 4 4 26.925
#> 3 6 3 19.750
#> 4 8 3 15.050
#> 5 4 3 21.500
#> 6 4 5 28.200
#> 7 8 5 15.400
#> 8 6 5 19.700
# Control grouping of result
mtcars |>
group_by_(~cyl, ~gear) |>
summarise_(mean_mpg = ~mean(mpg), .groups = "drop")
#> cyl gear mean_mpg
#> 1 4 3 21.500
#> 2 4 4 26.925
#> 3 4 5 28.200
#> 4 6 3 19.750
#> 5 6 4 19.750
#> 6 6 5 19.700
#> 7 8 3 15.050
#> 8 8 5 15.400
mtcars |>
group_by_(~cyl, ~gear) |>
summarise_(mean_mpg = ~mean(mpg), .groups = "keep")
#> cyl gear mean_mpg
#> 1 4 3 21.500
#> 2 4 4 26.925
#> 3 4 5 28.200
#> 4 6 3 19.750
#> 5 6 4 19.750
#> 6 6 5 19.700
#> 7 8 3 15.050
#> 8 8 5 15.400
#>
#> Grouped by: cyl, gear [8 | 1 (0)]
# Using standard evaluation (ungrouped data only)
mtcars |> summarise_(mean_mpg = mean(mtcars$mpg))
#> mean_mpg
#> 1 20.09062
# reframe_() for summaries returning multiple rows per group
mtcars |>
group_by_(~cyl) |>
reframe_(quantile_mpg = ~quantile(mpg, c(0.25, 0.5, 0.75)))
#> cyl quantile_mpg
#> 1 4 22.80
#> 2 4 26.00
#> 3 4 30.40
#> 4 6 18.65
#> 5 6 19.70
#> 6 6 21.00
#> 7 8 14.40
#> 8 8 15.20
#> 9 8 16.25
# Count observations by group
mtcars |> count_(~cyl)
#> cyl n
#> 1 4 11
#> 2 6 7
#> 3 8 14
# Count by multiple variables
mtcars |> count_(~cyl, ~gear)
#> cyl gear n
#> 1 4 3 1
#> 2 4 4 8
#> 3 4 5 2
#> 4 6 3 2
#> 5 6 4 4
#> 6 6 5 1
#> 7 8 3 12
#> 8 8 5 2
# Count with sorting
mtcars |> count_(~cyl, sort = TRUE)
#> cyl n
#> 1 8 14
#> 2 4 11
#> 3 6 7
# Count in increasing order
mtcars |> count_(~cyl, sort = TRUE, decreasing = FALSE)
#> cyl n
#> 1 6 7
#> 2 4 11
#> 3 8 14
# Count with weights
mtcars |> count_(~cyl, wt = ~mpg)
#> cyl n
#> 1 4 293.3
#> 2 6 138.2
#> 3 8 211.4
# Count with computed grouping variable
mtcars |> count_(high_mpg = ~mpg > 20)
#> high_mpg n
#> 1 FALSE 18
#> 2 TRUE 14
# Combine grouping and computation
mtcars |> count_(~cyl, high_hp = ~hp > 150)
#> cyl high_hp n
#> 1 4 FALSE 11
#> 2 6 FALSE 6
#> 3 6 TRUE 1
#> 4 8 FALSE 2
#> 5 8 TRUE 12
# tally_() - count rows (optionally by existing groups)
mtcars |> tally_()
#> n
#> Mazda RX4 32
mtcars |>
group_by_(~cyl) |>
tally_()
#> cyl n
#> 1 4 11
#> 2 6 7
#> 3 8 14
# tally with weights
mtcars |>
group_by_(~cyl) |>
tally_(wt = ~hp)
#> cyl n
#> 1 4 909
#> 2 6 856
#> 3 8 2929
# add_count_() - add count column without collapsing
mtcars |> add_count_(~cyl)
#> mpg cyl disp hp drat wt qsec vs am gear carb n
#> 1 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 7
#> 2 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 7
#> 3 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 11
#> 4 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 7
#> 5 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 14
#> 6 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 7
#> 7 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4 14
#> 8 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2 11
#> 9 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2 11
#> 10 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 7
#> 11 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4 7
#> 12 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3 14
#> 13 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3 14
#> 14 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3 14
#> 15 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4 14
#> 16 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4 14
#> 17 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4 14
#> 18 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1 11
#> 19 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2 11
#> 20 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1 11
#> 21 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1 11
#> 22 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2 14
#> 23 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2 14
#> 24 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4 14
#> 25 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2 14
#> 26 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1 11
#> 27 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2 11
#> 28 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2 11
#> 29 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4 14
#> 30 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6 7
#> 31 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8 14
#> 32 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2 11
# add_count with custom column name
mtcars |> add_count_(~cyl, name = "n_cyl")
#> mpg cyl disp hp drat wt qsec vs am gear carb n_cyl
#> 1 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 7
#> 2 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 7
#> 3 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 11
#> 4 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 7
#> 5 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 14
#> 6 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 7
#> 7 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4 14
#> 8 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2 11
#> 9 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2 11
#> 10 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 7
#> 11 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4 7
#> 12 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3 14
#> 13 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3 14
#> 14 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3 14
#> 15 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4 14
#> 16 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4 14
#> 17 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4 14
#> 18 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1 11
#> 19 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2 11
#> 20 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1 11
#> 21 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1 11
#> 22 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2 14
#> 23 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2 14
#> 24 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4 14
#> 25 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2 14
#> 26 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1 11
#> 27 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2 11
#> 28 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2 11
#> 29 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4 14
#> 30 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6 7
#> 31 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8 14
#> 32 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2 11
# add_count by multiple variables
mtcars |> add_count_(~cyl, ~gear)
#> mpg cyl disp hp drat wt qsec vs am gear carb n
#> 1 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 4
#> 2 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 4
#> 3 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 8
#> 4 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 2
#> 5 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 12
#> 6 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 2
#> 7 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4 12
#> 8 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2 8
#> 9 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2 8
#> 10 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 4
#> 11 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4 4
#> 12 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3 12
#> 13 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3 12
#> 14 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3 12
#> 15 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4 12
#> 16 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4 12
#> 17 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4 12
#> 18 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1 8
#> 19 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2 8
#> 20 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1 8
#> 21 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1 1
#> 22 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2 12
#> 23 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2 12
#> 24 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4 12
#> 25 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2 12
#> 26 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1 8
#> 27 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2 2
#> 28 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2 2
#> 29 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4 2
#> 30 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6 1
#> 31 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8 2
#> 32 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2 8
# add_tally_() - add total count to each row
mtcars |> add_tally_()
#> mpg cyl disp hp drat wt qsec vs am gear carb n
#> Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 32
#> Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 32
#> Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 32
#> Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 32
#> Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 32
#> Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 32
#> Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4 32
#> Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2 32
#> Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2 32
#> Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 32
#> Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4 32
#> Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3 32
#> Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3 32
#> Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3 32
#> Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4 32
#> Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4 32
#> Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4 32
#> Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1 32
#> Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2 32
#> Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1 32
#> Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1 32
#> Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2 32
#> AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2 32
#> Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4 32
#> Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2 32
#> Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1 32
#> Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2 32
#> Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2 32
#> Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4 32
#> Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6 32
#> Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8 32
#> Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2 32
mtcars |>
group_by_(~cyl) |>
add_tally_()
#> mpg cyl disp hp drat wt qsec vs am gear carb n
#> 1 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 7
#> 2 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 7
#> 3 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 11
#> 4 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 7
#> 5 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 14
#> 6 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 7
#> 7 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4 14
#> 8 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2 11
#> 9 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2 11
#> 10 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 7
#> 11 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4 7
#> 12 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3 14
#> 13 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3 14
#> 14 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3 14
#> 15 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4 14
#> 16 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4 14
#> 17 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4 14
#> 18 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1 11
#> 19 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2 11
#> 20 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1 11
#> 21 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1 11
#> 22 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2 14
#> 23 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2 14
#> 24 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4 14
#> 25 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2 14
#> 26 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1 11
#> 27 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2 11
#> 28 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2 11
#> 29 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4 14
#> 30 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6 7
#> 31 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8 14
#> 32 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2 11
# Chain operations
mtcars |>
count_(~cyl, ~gear, sort = TRUE) |>
mutate_(pct = ~n/sum(n) * 100)
#> cyl gear n pct
#> 1 8 3 12 37.500
#> 2 4 4 8 25.000
#> 3 6 4 4 12.500
#> 4 4 5 2 6.250
#> 5 6 3 2 6.250
#> 6 8 5 2 6.250
#> 7 4 3 1 3.125
#> 8 6 5 1 3.125
# Use with filtering
mtcars |>
add_count_(~cyl) |>
filter_(~n > 10)
#> mpg cyl disp hp drat wt qsec vs am gear carb n
#> 1 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 11
#> 2 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 14
#> 3 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4 14
#> 4 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2 11
#> 5 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2 11
#> 6 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3 14
#> 7 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3 14
#> 8 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3 14
#> 9 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4 14
#> 10 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4 14
#> 11 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4 14
#> 12 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1 11
#> 13 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2 11
#> 14 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1 11
#> 15 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1 11
#> 16 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2 14
#> 17 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2 14
#> 18 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4 14
#> 19 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2 14
#> 20 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1 11
#> 21 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2 11
#> 22 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2 11
#> 23 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4 14
#> 24 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8 14
#> 25 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2 11