0

My current code works (thanks to R input names from a character vector to a function formula statement) but I wonder if there is a more elegant solution to my problem.

Given the following data, I would like to run t-tests to compare means between all possible combinations of group and result variables.

library(tidyverse)

dat <- tibble(
  result1 = rnorm(20),
  result2 = rnorm(20),
  group1 = sample(0:1, 20, replace = TRUE),
  group2 = sample(0:1, 20, replace = TRUE)
)

results <- c("result1", "result2")
groups <- c("group1", "group2")
b <- crossing(results, groups)

map2(b$results, b$groups, ~ t.test(as.formula(paste(.x, "~", .y)), data = dat))

Is there an alternative to supplying character vectors and writing as.formula each time?

Sam
  • 307
  • 1
  • 8

1 Answers1

0

If you're fine with using {dplyr} and {tidyr}, you could:

library(dplyr)
library(tidyr)

dat |>
  pivot_longer(cols = starts_with('group'), names_to = 'group') |>
  select(-'value') |>
  pivot_longer(cols = starts_with('result'), names_to = 'result') |>
  group_by(group) |>
  summarize(t.result = list(t.test(value ~ result)[c('p.value', 'statistic')])) |>
  unnest_wider(t.result)
# A tibble: 2 x 3
  group  p.value statistic
  <chr>    <dbl>     <dbl>
1 group1  0.0852     -1.77
2 group2  0.0852     -1.77
I_O
  • 4,983
  • 2
  • 2
  • 15