-1

I am trying to create a loop which imports multiple files and merges them. I would like to find a way that during this process a new column is created called "filename" which contains the name of the input file that each row has come from.Is this possible?

Sean
  • 45
  • 5
  • Save the file name as a variable, in a vector of strings. Loop over each element of the vector of stings creating then naming a separate column vector and concatenating it to a matrix that was initialized as an empty matrix each time. – user4933 Feb 12 '21 at 17:43
  • 1
    Hope this helps :) – user4933 Feb 12 '21 at 17:46

1 Answers1

1
my_data <- lapply(setNames(nm=my_files), read.csv)
my_data <- Map(function(x, nm), { x$filename <- nm; x; },
               my_data, my_files)
# or much more succinctly
my_data <- Map(transform, my_data, filename = my_files)

The first step (lapply) will produce a list of frames (see https://stackoverflow.com/a/24376207/3358227). While not required, I also named the list with the filename. This might be handy at times, but is generally subsumed by the second step, adding the filename to the data.

The second step uses Map; lapply is to one list/vector what Map is to one-or-more, see https://stackoverflow.com/a/54485425/3358272 for a walk-through of "unrolling" the call to Map. This second step just adds a column to each frame, and returns the frame.

After that, my_data will be a named list where the filename is both the name of the list element, and in a column $filename in each frame.

r2evans
  • 77,184
  • 4
  • 55
  • 96