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.")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))
#> # A tibble: 3 × 11
#> dose estimate estimate1 estimate2 statistic p.value parameter conf.low
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 0.5 5.25 13.2 7.98 3.17 0.00636 15.0 1.72
#> 2 1 5.93 22.7 16.8 4.03 0.00104 15.4 2.80
#> 3 2 -0.0800 26.1 26.1 -0.0461 0.964 14.0 -3.80
#> # ℹ 3 more variables: conf.high <dbl>, method <chr>, alternative <chr>
# Or this
ToothGrowth %>%
group_by(dose) %>%
doo(stat_test, len ~ supp)
#> # A tibble: 3 × 11
#> dose estimate estimate1 estimate2 statistic p.value parameter conf.low
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 0.5 5.25 13.2 7.98 3.17 0.00636 15.0 1.72
#> 2 1 5.93 22.7 16.8 4.03 0.00104 15.4 2.80
#> 3 2 -0.0800 26.1 26.1 -0.0461 0.964 14.0 -3.80
#> # ℹ 3 more variables: conf.high <dbl>, method <chr>, alternative <chr>
# 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
#> # A tibble: 3 × 2
#> dose .results.
#> <dbl> <list>
#> 1 0.5 <htest>
#> 2 1 <htest>
#> 3 2 <htest>
comparisons$.results.
#> [[1]]
#>
#> Welch Two Sample t-test
#>
#> data: len by supp
#> t = 3.1697, df = 14.969, p-value = 0.006359
#> alternative hypothesis: true difference in means between group OJ and group VC is not equal to 0
#> 95 percent confidence interval:
#> 1.719057 8.780943
#> sample estimates:
#> mean in group OJ mean in group VC
#> 13.23 7.98
#>
#>
#> [[2]]
#>
#> Welch Two Sample t-test
#>
#> data: len by supp
#> t = 4.0328, df = 15.358, p-value = 0.001038
#> alternative hypothesis: true difference in means between group OJ and group VC is not equal to 0
#> 95 percent confidence interval:
#> 2.802148 9.057852
#> sample estimates:
#> mean in group OJ mean in group VC
#> 22.70 16.77
#>
#>
#> [[3]]
#>
#> Welch Two Sample t-test
#>
#> data: len by supp
#> t = -0.046136, df = 14.04, p-value = 0.9639
#> alternative hypothesis: true difference in means between group OJ and group VC is not equal to 0
#> 95 percent confidence interval:
#> -3.79807 3.63807
#> sample estimates:
#> mean in group OJ mean in group VC
#> 26.06 26.14
#>
#>
# Example 3: R base function combined with tidy()
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ToothGrowth %>%
group_by(dose) %>%
doo(~t.test(len ~ supp, data =.) %>% tidy())
#> # A tibble: 3 × 11
#> dose estimate estimate1 estimate2 statistic p.value parameter conf.low
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 0.5 5.25 13.2 7.98 3.17 0.00636 15.0 1.72
#> 2 1 5.93 22.7 16.8 4.03 0.00104 15.4 2.80
#> 3 2 -0.0800 26.1 26.1 -0.0461 0.964 14.0 -3.80
#> # ℹ 3 more variables: conf.high <dbl>, method <chr>, alternative <chr>