I would like to import all .csv files in a directory and combine them into a single file using
Join. I can do this manually but when I try to automate it using
&/@ I run into problems.
The datafiles and example Mathematica notebook described in this question is available at this google drive link
There are 7 files in the notebook directory, each with 6 header rows and ten rows of data. (Each row has 8 columns).
I use the code below to do the following:
- Search a directory for all file names containing ".csv"
- Import them
- Remove the header from each file.
root = NotebookDirectory; headerROWS = 6; filenames = FileNames["*.csv", root]; files = Import[#] & /@ filenames; files2 = Drop[#, headerROWS] & /@ files;
I verify that everything imported corrected as shown in screenshot below (7 files, each with 10 rows of data).
Stupid Manual Solution
Using the same placeholder
Join, it's straightforward but annoying to accomplish what I want manually:
MANUALjoin = Join[files2[], files2[], files2[], files2[], files2[], files2[], files2[]]; (*the dumb, manual way I want to avoid*)
The screenshot below shows this working as expected, i.e.: it simply concatenates the files while retaining the structure of the individual files.
When I try to automate the
Join operation using code analogous to the &/@ in the first block of code, it jumbles the placement of the rows. This is what I've tried.
AUTOjoin = Join[#] & /@ files2; (*Fails *)
I see the following jumbled output:
It seems that
[#] is somehow incompatible with
Join but I don't see exactly how. It seems that this code should be identical to the 'manual' version above. Does someone see what the problem is exactly?