Given a data frame containing two repeated measures of different variables (i.e. A1, A2, B1, B2
)
library(purrr)
library(tidyr)
library(broom)
set.seed(123)
my_df = data.frame(matrix(rnorm(80), nrow=10))
colnames(my_df) <- c("A1_BEFORE", "A1_AFTER", "A2_BEFORE", "A2_AFTER",
"B1_BEFORE", "B1_AFTER", "B2_BEFORE", "B2_AFTER")
How can I use functional programming principles to iterate over pairs (BEFORE, AFTER) of the same variables, and obtain a "tidy" result? Here's my attempt:
bef <- select(my_df, contains("BEFORE"))
aft <- select(my_df, contains("AFTER"))
result <- map2(bef, aft, t.test, paired = T)
The above results in multiple nested lists. How could I obtain a "tidy" result?
result <- tidy(map2(bef, aft, t.test, paired = T))
result <- tidy(map2(bef, aft, t.test, paired = T))
Error in tidy.list(map2(bef, aft, t.test, paired = T)) : No tidying method recognized for this list In addition: Warning message: In sort(names(x)) == c("d", "u", "v") : longer object length is not a multiple of shorter object length