merge multiple csv into one df in r

Hi everyone.
I can upload each csv file one by one(read_csv) and then merge them using rbind but perhaps there is more efficient way to do it? instead of typing read.csv 12 times.

bike_rides <- rbind(apr_2020, may_2020, jun_2020,jul_2020,aug_2020,sep_2020,oct_2020,
purrr::map_dfr(list_of_your_filenames, read_csv)
1 Like

instead of naming all file names can I just indicate the directory and it will upload all files from that directory? Your method is convenient and super easy.

Just use list.files() or dir() to select the files. Check the parameters for the functions for the correct options.

i get this mistake

when i upload files using read.csv one by one it works. but now it says that a file is missing but it is not

Just check an example, e.g.

1 Like

I used this code and it worked perfectly

temp <- list.files ("C:/Users/Desktop", full.names = TRUE, pattern = "\.csv$")
bike_rides <- rbindlist(lapply(temp, fread), fill = TRUE)

That's exactly the same using data.table as the version I gave you. You originally asked for read_csv, so I gave you the purrr solution.

1 Like

yes man. you are right. I also noticed that if we have two identical columns in two data frames and those columns have different data types then dplyr package will not combine them but data.table will. I believe, that is a great advantage to use data.table instead dplyr because after merging i can change the data type at once.

This should also work, assuming all files have the same structure.

temp <- list.files("C:/Users/Desktop", full.names = TRUE, pattern = "\\.csv$")

bike_rides <- readr::read_csv(temp, id = "file_name")

Great call. I forgot about that relatively recent upgrade in convenience.

This topic was automatically closed 7 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.