Skip to content

Instantly share code, notes, and snippets.

@brshallo
Last active November 25, 2021 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brshallo/01496b68adef88a71de1fd44f3712b10 to your computer and use it in GitHub Desktop.
Save brshallo/01496b68adef88a71de1fd44f3712b10 to your computer and use it in GitHub Desktop.

Using pwiser

library(tidyverse)
if (!require(pwiser) ) remotes::install_github("brshallo/pwiser")

penguins <- palmerpenguins::penguins %>% na.omit()

t_test_statistic <- function(x, y){
  t.test(x, y) %>% 
    broom::tidy() %>% 
    pull(statistic)
}

penguins %>% 
  group_by(species, year) %>% 
  summarise(flipper_length_mm = list(flipper_length_mm)) %>% 
  pivot_wider(names_from = year,
              values_from = flipper_length_mm) %>% 
  rowwise() %>% 
  mutate(
    pwiser::pairwise(where(is.list), 
                     .fns = t_test_statistic,
                     .is_commutative = TRUE),
    .keep = "unused"
  )
#> # A tibble: 3 x 4
#> # Rowwise:  species
#>   species   `2007_2008` `2007_2009` `2008_2009`
#>   <fct>           <dbl>       <dbl>       <dbl>
#> 1 Adelie          -3.40       -4.10      -0.878
#> 2 Chinstrap       -2.54       -3.07      -0.162
#> 3 Gentoo          -1.85       -2.33      -0.588
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment