doo | R Documentation |
Provides a flexible alternative to the dplyr:do()
function.
Technically it uses nest() + mutate() + map()
to apply arbitrary
computation to a grouped data frame.
The output is a data frame. If the applied function returns a data frame, then the output will be automatically unnested. Otherwise, the output includes the grouping variables and a column named ".results." (by default), which is a "list-columns" containing the results for group combinations.
doo(data, .f, ..., result = ".results.")
data |
a (grouped) data frame |
.f |
A function, formula, or atomic vector. For example
|
... |
Additional arguments passed on to .f |
result |
the column name to hold the results. Default is ".results.". |
a data frame
# Custom function #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% stat_test <- function(data, formula){ t.test(formula, data) %>% tidy() } # Example 1: pipe-friendly stat_test(). # Two possibilities of usage are available #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% # Use this ToothGrowth %>% group_by(dose) %>% doo(~stat_test(data =., len ~ supp)) # Or this ToothGrowth %>% group_by(dose) %>% doo(stat_test, len ~ supp) # Example 2: R base function t.test() (not pipe friendly) # One possibility of usage #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% comparisons <- ToothGrowth %>% group_by(dose) %>% doo(~t.test(len ~ supp, data =.)) comparisons comparisons$.results. # Example 3: R base function combined with tidy() #%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ToothGrowth %>% group_by(dose) %>% doo(~t.test(len ~ supp, data =.) %>% tidy())
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.