I am trying to read 1000's of csv file & append them and save them as one rds file.
Issue: I am trying to add the filename as a column in each csv
so that I know what data has come from which csv file (All files have same columns) but not able to do so.
Example csv's to work with:
# Setting up some example csv files to work with
mtcars_slim <- select(mtcars, 1:3)
write_csv(slice(mtcars_slim, 1:4), "Input data/sub folder/AA_1.csv")
write_csv(slice(mtcars_slim, 5:10), "Input data/sub folder/AAA_2.csv")
write_csv(slice(mtcars_slim, 11:1), "Input data/sub folder/BBB_3.csv")
Code I have tried below:
# this code worked but it doesn't have filename within the dataset
list.files(path = "Input data/sub folder/",
pattern="*.csv",
full.names = T) %>%
map_df(~read_csv(.)) %>%
saveRDS("output_compiled_data.rds")
So I have tried to modify above code to include filename as column to each csv file in below code chunk but it didn't work.
file_names <- list.files(path = "Input data/sub folder/",
pattern="*.csv",
full.names = T) %>%
map_df(file_names, ~read_csv(.) %>%
mutate(symbol = file_names)) %>%
saveRDS("output_compiled_data.rds")
data_tbl <- read_rds("output_compiled_data.rds")
data_tbl