Hello,
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.
This is what my code looks like so far:
install.packages("tidyverse", "readxl")
library(tidyverse)
library(readxl)
files <- list.files(
path = "my path",
pattern = ".xlsx",
full.names = TRUE)
all_file_contents <- map(.x = files,
.f = ~ {
temp_file <- .x
map(.x = excel_sheets(.x),
.f = ~ read_excel(path = temp_file,
sheet = .x) %>%
mutate(sheet_name = .x))
})
Data_collated <- transpose(all_file_contents) %>%
map_df(.x = .,
.f = ~ .x)
I have tried adding in the following at various locations but no luck yet...:
x$files = i
Any help would be greatly appreciated! Thanks in advance!