Combining CSV files

Hi everyone. I am a complete coding newbie so I am really quite lost trying to combine CSV data sets together. After setting my required folder to the working directory, the only line of code I inserted is:

Following this, to combine the files, I used this:"rbind",lapply(files,read.csv,header=TRUE,fill=TRUE))

And this is what I get:
argument "args" is missing, with no default

I'd really appreciate any help understanding what I need to fix this!

You can follow this general pattern


list_of_files <- list.files(path = "path/to/your/files",
                            pattern = "\\.csv$",
                            full.names = TRUE)
df <- list_of_files %>% 
  map_dfr(read.csv, header=TRUE, fill=TRUE)

You can use, rbind to combine files with same column headers and bindrows to combine the files with different column headers. This logic is apt for all types of files.

This is a little misleading, you can combine data frames not files, you have to read the files into memory first.

Just for reference, there's a very good (and fairly exhaustive) set of answers to this in the StackOverflow thread I've linked below:

Not all of them combine the csvs, but several do (see, e.g., answers with data.table::rbindlist()).

Ya, to be more clear and elaborate with my previous answer, we would be importing the file as dataframe, and if these dataframes are with same column headers, then we would be using rbind to combine all the dataframes. And, if the dataframes are with different column headers, then we would be using bind rows to combine all the dataframes. After combining the dataframes, we can use write_csv to write that particular combined dataframe as file. We could even use write.csv to write the dataframe as a file, but, write.csv would add an index number to the written file. So, preferably, we can use write_csv to write the combined_dataframe as a file.

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