Set custom summaries to be performed within a descriptive statistics layerSource:
This function allows a user to define custom summaries to be performed in a
dplyr::summarize(). A custom summary by the same name as a
default summary will override the default. This allows the user to override
the default behavior of summaries built into 'Tplyr', while also adding new
desired summary functions.
desclayer on which the summaries should be bound
Named parameters containing syntax to be used in a call to
When programming the logic of the summary function, use the variable name
.var to within your summary functions. This allows you apply the
summary function to each variable when multiple target variables are
An important, yet not immediately obvious, part of using
set_custom_summaries is to understand the link between the named
parameters you set in
set_custom_summaries and the names called in
f_str objects within
f_str, after you supply the string format you'd like your
numbers to take, you specify the summaries that fill those strings.
When you go to set your format strings, the name you use to declare a summary
set_custom_summaries is the same name that you use in your
f_str call. This is necessary because
set_format_strings needs some means of putting two summaries in
the same value, and setting a row label for the summary being performed.
#Load in pipe library(magrittr) tplyr_table(iris, Species) %>% add_layer( group_desc(Sepal.Length, by = "Sepal Length") %>% set_custom_summaries( geometric_mean = exp(sum(log(.var[.var > 0]), na.rm=TRUE) / length(.var)) ) %>% set_format_strings( 'Geometric Mean' = f_str('xx.xx', geometric_mean) ) ) %>% build() #> # A tibble: 1 × 8 #> row_label1 row_label2 var1_set…¹ var1_…² var1_…³ ord_l…⁴ ord_l…⁵ ord_l…⁶ #> <chr> <chr> <chr> <chr> <chr> <int> <int> <int> #> 1 Sepal Length Geometric Mean " 4.99" " 5.91" " 6.56" 1 1 1 #> # … with abbreviated variable names ¹var1_setosa, ²var1_versicolor, #> # ³var1_virginica, ⁴ord_layer_index, ⁵ord_layer_1, ⁶ord_layer_2