I have multiple excel files that I need to combine into one dataframe.
The files have several sheets (exact same number of sheets, column headers, number of observations).
I have managed to merge them into one dataset based on the information in thise very helpful post:
My files are named 100.xlsx, 101.xlsx, 102.xlsx etc. When reading the files into R and combining them into one dataframe I would like to add a separate column named "subject ID" corresponding to the name of the individual files being uploaded.
Thanks a lot for the reply!
I have managed to add file names to the data. However, now I cannot transpose the data to get all the sheets into one df, as two of my sheets have less observations. Before adding in the file names, this was not a problem and I deleted them after.
Is there a way to specify which sheets to extract from the files (in my code above) rather than R extracting all files?
I have tried adding in '2:7' at various places but it isn't happy with that
I think you are close. I have a small recommendation. Write a function to read a single sheet read_tab <- function(filename, sheetname) that writes the sheetname as a column. Then write a function to read a single file that maps across the sheets. Then map that function over the files. In some cases you may need map2 or imap.
I sense that trying to do so much with the inline ~ functions is making the problem more difficult for you.