Importing multiple csv files and add year to each file

Viewed 134

I'm using the following function to import roughly 10 files from different years (all from the same folder) into r.

ed.attain.places_0_10_18 <-
    list.files(path = "./ed_attainment/",
               pattern = "*.csv", 
               full.names = T) %>% 
    map_df(~read_csv(., col_types = cols(.default = "c"))) 

This folder (ed_attainment/) contains the following csv files all the way to '17.

ACS_09_5YR_B19301_with_ann.csv
ACS_10_5YR_B19301_with_ann.csv

I want to add the year to each one of these files within the above function. Just importing one csv file the code would be

read_csv("ACS_09_5YR_B19301_with_ann.csv")%>%mutate(year = 09)

such that the year in the filename corresponds with the values in the new "year" variable.

Thanks much!

2 Answers

I suggest:

myReadCsv <- function(fullPath){
   # Pull the Year out of the fullPath
  year <- paste0("20", str_match(fullPath, 
                         "ACS_(\\d{2})")[,2])
    # Read csv as you did before
  read_csv(fullPath, col_types = cols(.default = "c")) %>% 
     # Add YEAR in as a column
    mutate(YEAR = year)
}

and then

ed.attain.places_0_10_18 <-
  list.files(path = "./ed_attainment/",
             pattern = "*.csv", 
             full.names = T) %>% 
  map_df(myReadCsv) 

NOT RUN

## Because I don't have a directory of CSV files handy. ##

There are various ways to do this but without changing much of your code, you can add an id variable named year in map_df which will have index of the filename. So first file (ACS_09_5YR_B19301_with_ann.csv) would have index as 1, second file (ACS_10_5YR_B19301_with_ann.csv) would have index as 2 and so on.

You can then add 2008 to this index to get year value from 2009-2017.

list.files(path = "./ed_attainment/",
           pattern = "\\.csv", 
           full.names = TRUE) %>% 
    purrr::map_df(~readr::read_csv(.,col_types = cols(.default = "c")), 
                 .id ='year') %>% 
   dplyr::mutate(year = 2008 + as.integer(year))
Related