I have multiple excel files and they have unique sheet names (date of file creation in my case). I read them in bulk and need to assign the sheet name to each file in new column "id". I know how to make numeric id, or id = file name, but cannot find a way to get sheet name as id.
library(readxl)
library(data.table)
file.list <- list.files("C:/Users/.../Studies/",pattern='*.xlsx')
df.list <- lapply(file.list, read_excel)
#id = numeric
df <- rbindlist(df.list, idcol = "id")
#Or by file name:
attr(df.list, "names") <- file.list
df2 = rbindlist(df.list,idcol="id")
#How to get sheet names?
attr(df.list, "names") <- sapply(file.list, excel_sheets)
? Should work if each file has only one sheet. - Abdoudf.list
does not contains the right data. If so, you can do:attr(df.list, "names") <- sapply(file.list, function(x) excel_sheets(x)[1])
. - Abdou