0

I'm reading this SO post.

The top voted answer for reading multiple csv files into a single dataframe is:

file_names <- dir() #where you have your files
your_data_frame <- do.call(rbind,lapply(file_names,read.csv))

My question is, suppose I have 5 files and I'd like to add a column on the new dataframe indicating which order the file was read in, 1 through 5. How can I do that? Do I have to doa. for loop using an iterator i or is there a neat way to encompass with the short code block above?

Doug Fir
  • 19,971
  • 47
  • 169
  • 299
  • Use `data.table::rbindlist` with it's argument `idcol = TRUE`. – pogibas Jan 12 '19 at 00:24
  • I would suggest using `rbindlist(lapply(files, fread), idcol = TRUE)` command from the linked post (uses `fread` from the `data.table` package) – pogibas Jan 12 '19 at 00:26
  • Thanks for the suggestion, I'll use data.table as backup but would prefer tidyverse or base r if there's a solution out there? – Doug Fir Jan 12 '19 at 00:27

0 Answers0