Calculates aggregates in pivot tables

pvt(df, rows = NULL, cols = NULL, ..., .totals = "Total", .keep_total = "all")

Arguments

df

A dataframe

rows

A vector of variable names that will be on the rows of the pivot table

cols

A vector of variable names that will be on the columns of the pivot table

...

calculations that are passed to `dplyr::summarise()` via the `pivotr::cube()` function

.totals

A string of how output totals are labeled in the rows and cols

.keep_total

one of "all", "overall", "none"

Value

a tibble

Examples

pvt(mtcars, N = n())
#> # A tibble: 1 x 1 #> N #> <int> #> 1 32
pvt(mtcars, cyl, am, N = n(), Avg = mean(mpg))
#> # A tibble: 4 x 7 #> cyl N_0 N_1 N_Total Avg_0 Avg_1 Avg_Total #> <chr> <int> <int> <int> <dbl> <dbl> <dbl> #> 1 4 3 8 11 22.9 28.1 26.7 #> 2 6 4 3 7 19.1 20.6 19.7 #> 3 8 12 2 14 15.0 15.4 15.1 #> 4 Total 19 13 32 17.1 24.4 20.1
pvt(mtcars, cyl, am, Avg = mean(wt), N = n())
#> # A tibble: 4 x 7 #> cyl Avg_0 Avg_1 Avg_Total N_0 N_1 N_Total #> <chr> <dbl> <dbl> <dbl> <int> <int> <int> #> 1 4 2.94 2.04 2.29 3 8 11 #> 2 6 3.39 2.76 3.12 4 3 7 #> 3 8 4.10 3.37 4.00 12 2 14 #> 4 Total 3.77 2.41 3.22 19 13 32
pvt(mtcars, NULL, am, Avg = mean(wt))
#> # A tibble: 1 x 3 #> `0` `1` Total #> <dbl> <dbl> <dbl> #> 1 3.77 2.41 3.22
pvt(mtcars, c(vs, am), cyl, N = n(), .keep_total = "overall")
#> # A tibble: 5 x 6 #> vs am `8` `4` `6` Total #> <chr> <chr> <int> <int> <int> <int> #> 1 0 0 12 NA NA 12 #> 2 0 1 2 1 3 6 #> 3 1 0 NA 3 4 7 #> 4 1 1 NA 7 NA 7 #> 5 Total Total 14 11 7 32