The file that was given contains 8 dataframes named data1, data2,...,data8. I need to perform t.test()on each and organize the results in a table.
t1 <- t.test(A ~ B, data1)
t2 <- t.test(A ~ B, data2)
.
.
.
t8 <- t.test(A ~ B, data8)
summary_table <- map_df(list(t1, t2, t3, t4, t5, t6, t7, t8), tidy)
Is there any way of shortening this code perhaps using list or lapply? Dataframe names are repetitive and has a pattern so I don't want to repeat writing it also.
CodePudding user response:
Use mget
to load the data in a list
, loop over the list
with map/lapply
, apply the t.test
and return the tidy
output as a single data.frame with a column to identify the origin data with .id
in map
library(purrr)
library(broom)
mget(paste0("data", 1:8)) %>%
map_dfr(~ t.test(A ~ B, data = .x) %>%
tidy, .id = 'dataname')