CSVs Across Directories

Reminder

Please use all code samples responsibly - these are samples and likely require adjustments to work correctly for your specific needs. Read through the documentation and comments to understand any caveats or limitations of the code and/or data and follow-up with the code author or Code Library admins (code_library@urban.org) if you have questions on how to adapt the sample to your specific use case.

Purpose: This code is used to read in multiple csvs from multiple folders and append them together. This is useful if you get data from an outside partner and want to restructure the files. For example, you may have quarterly data separated out in individual folders that you want to combine into one file.

Data: This code is relevant for any csv files (although could be amended for a different file type), that are spread across folders that you want to combine. NOTE: the way that this code sample is written, it will combine all files with a .csv file type. You may want to specify a more specific pattern to be more discerning with what you combine - see the regular expression tutorial for more information on how to do this.

Author: Fay Walker (January 2023)

### function to read in the csvs from across directories
#these are the names of the folders you want to pull in, 
#this is also what you will name the dfs that are created from the csvs in each folder
directory_names <- c('activities','assessment','case_notes','demographics',
                     'response_and_change','sessions')

#this sets the directory, pulls all the csvs in that directory
read_dir <- function(dir_name){
    
  directory <- paste("DIRECTORY",dir_name, sep="")
  
  setwd(directory)
  # NOTE: this will combine all files with a .csv file type
  # you may want to specify a more specific pattern to be more
  # discerning with what you combine - see the regular expression 
  # tutorial for more information on how to do this
  file_name <- dir(pattern = "*.csv")   
  
  #created one single rbinded csv of all the csvs in said directory
  appended_data <-  file_name %>%
    # map applies the read_csv function to each file in the list
    map(read_csv) %>%
    reduce(rbind) %>%
    clean_names()
  #name that csv x_data, save it to your environment
  df_name <- paste(dir_name, "_data", sep="")
  assign(df_name, appended_data, envir = .GlobalEnv)
}

#do this for all the directory names you named up top
map(directory_names, read_dir)