How to add a index by set of data when using rbindlist?

This is an enhanced version of Nicolás’ answer which adds the file names instead of numbers:

x2csv <- rbindlist(lapply(files, fread), idcol = "origin")
x2csv[, origin := factor(origin, labels = basename(files))]
  • fread() uses stringsAsFactors = FALSE by default so we can save some keystrokes
  • Also fill = TRUE is only required if we want to read files with differing structure, e.g., differing position, name, or number of columns
  • The id col can be named (the default is .id) and is populated with the sequence number of the list element.
  • Then, this number is converted into a factor whose levels are labeled with the file names. A file name might be easier to remember than just a mere number. basename() strips the path off the file name.

Leave a Comment