The directories' structure is:
data -> topic1 -> question1 -> sheetName.csv
-> question2 -> sheetName.csv
...
-> topic2 -> question1 -> sheetName.csv
-> question2 -> sheetName.csv
...
The output I want an an excel file for each 'topic'. In each file, there are sheets that correpsond to the sheetName.csv within that topic. E.g. an excel file named: topic1.xlsx with 3 sheets, coresponding to 3 sheetName.csv files in topic 1.
BUT I also want to keep the sheet names as in the original .csv files. Note that the 'sheetName' is random (i.e. not follow any pattern).
Here are the codes I have tried so far:
library(readxl)
library(writexl)
library(dplyr)
pathName <- "/data/"
topicName <- list.files(path = pathName)
for(i in 1:length(topicName)) {
topicPath <- paste(pathName, topicName[[i]], sep = "")
files_to_read = list.files(
path = topicPath,
pattern = '*.csv',
recursive = TRUE,
full.names = TRUE
)
data_lst <- list()
data_lst <- lapply(files_to_read, read.csv)
setwd(pathName)
write_xlsx(data_lst, path = paste(topicName[[i]], ".", "xlsx", sep = ""))
}
The output I got is an excel file for each topic with the corresponding csv sheets, but the sheetnames are "sheet 1, sheet 2, etc...". Is there a way to keep the sheet names while writing to an excel file?