Having trouble figuring out how to do a series of t tests in a for loop and take the outputs each time the test is completed and append the results to a data frame. The goal is to run many t-tests at once and produce a data frame of all the results.
Here's it done with the mtcars dataset the slow way:
library(tidyverse)
library(rstatix)
# T-test to determine if there is a significant difference between mpg of
# automatic vs manual transmissions (automatic=0, manual=1)
t1 <- mtcars %>%
t_test(mpg ~ am) %>%
mutate(var = "am") # add lable to merge by
# Calculate mean mpg of both groups
t1.1 <- mtcars %>%
group_by(am) %>%
summarize(Mean = mean(mpg, na.rm=TRUE)) %>%
pivot_wider(names_from = am, values_from = Mean) %>% # Bring to wide format to add to df
mutate(var = "am") # add label to merge by
# T-test for vs (v-shape=0, straight line=1)
t2 <- mtcars %>%
t_test(mpg ~ vs) %>%
mutate(var = "vs") # add lable to merge by
# Calculate mean mpg of both groups
t2.1 <- mtcars %>%
group_by(vs) %>%
summarize(Mean = mean(mpg, na.rm=TRUE)) %>%
pivot_wider(names_from = vs, values_from = Mean) %>% # Bring to wide format to add to df
mutate(var = "vs") # add label to merge by
# Merge dfs and rename
t_bind <- rbind(t1, t2)
t.1_bind <- rbind(t1.1, t2.1)
t.1_bind <- t.1_bind %>% rename("mean_0" = "0", "mean_1" = "1")
t_merge <- merge(t_bind, t.1_bind, by = "var")
But when I try to set this up for a loop, I'm lost. Seems like this should be fairly simple, just not thinking about it right
t_vars <- c("am", "vs") # etc.
for (i in t_vars) {
x1 <- mtcars %>%
t_test(mpg ~ i) %>%
mutate(var = colnames(mpg[[i]]))
df <- append(x1)
}
# Error: Can't extract columns that don't exist.
# x Column `i` doesn't exist.
Thank you for the help!!