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?