generate for loop for different csv files

This might not be exactly what you are looking for, but based on what you are trying to achieve, it might be a good approach to look into:

# Load Libraries ----------------------------------------------------------
library("tidyverse")
library("broom")


# Create Example Data -----------------------------------------------------
map(1:6, \(i) write_csv(x = tibble(x = rnorm(10), y = rnorm(10)),
                        file = str_c("~/my_csv_file_", i, ".csv")) )


# Load Data ---------------------------------------------------------------
my_csv_files <- list.files(path = "~", full.names = TRUE, pattern = "csv$")
my_data <- my_csv_files %>% 
  map(read_csv)


# Run tests ---------------------------------------------------------------
my_test_results <- my_data %>% 
  bind_rows(.id = "file_number") %>% 
  group_by(file_number) %>% 
  nest %>% 
  ungroup %>% 
  mutate(test = map(data, ~t.test(pluck(.x, "x"),
                                  pluck(.x, "y"), data = .x)),
         tidy_test = map(test, tidy)) %>% 
  unnest(tidy_test) %>% 
  select(-data, -test) %>% 
  mutate(q.value = p.adjust(p.value))

...and hot tip: Want to understand what is going on? Ask chatGPT Please explain the following code in simple terms and then paste the above :+1:

1 Like