Rstudio: Import and combine multiple .csv files and create multiple dataframes

I have multiple .csv files in a folder that I would like to combine into multiple dataframes. I already can do this for one set of files using the following code:

DF_week1 <- list.files(path = 'x:/full/file/path', 
pattern = "^Dai15E_ABC_10mbin_20211201_fullwatercolumn_evening_BNR*.*_week1.csv", full.names = TRUE) %>%
  map_dfr(read_csv)

I have standard file names like this for about 5 weeks worth of data. What I want to do is make a loop. For example, the first batch of file names ends in "week1" with the next set ending in "week2". I'm trying to write something that would recognize that the number after "week" has changed and to now combine those .csv files into a new data frame (DF_week2, DF_week3, etc.). I'm unfortunately a bit stuck here and rather new to looping.

Try something like the following. I didn't test it so there may be silly errors.

for( i in 1:5) {
  PAT <- paste0("^Dai15E_ABC_10mbin_20211201_fullwatercolumn_evening_BNR*.*_week",
                i,".csv")
  tmp <- list.files(path = 'x:/full/file/path', 
                    pattern = PAT, full.names = TRUE) %>%
    map_dfr(read_csv)
  assign[x = paste0("DF_week", i), value = tmp]
}

If all your files share the same column structure, you can read them all at once and then separate them into individual data frames, obviously, I can't test the code since I don't have access to your files but it would be something like this.

library(tidyverse)

all_weeks_list <- list.files(path = 'x:/full/file/path',
                            pattern = "week\\d\\.csv$",
                            full.names = TRUE)

all_weeks_list %>% 
    read_csv(id = "file_name") %>% 
    mutate(id = str_extract(id, "week\\d(?=\\.csv)")) %>% 
    group_nest(id) %>% 
    mutate(data = set_names(data, paste("DF_", id))) %>% 
    pull(data) %>% 
    list2env(envir = globalenv())

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.