Posts

Showing posts with the label readxl

Reading multiple xlsx files each with multiple sheets - purrr

Reading multiple xlsx files each with multiple sheets - purrr I have multiple excel files, each with different worksheets. I have tried to use readxl and map to import it to R. However, I was only able to do it using a for loop. The code below works fine but I would like to know if there is a clever way to do this. I keep thinking that I could have done it with map2 but I am missing something. library(tidyverse) library(readxl) library(writexl) ### As a first step, I get all the files from my project folder and create an empty list for looping purposes files <- list.files(pattern = ".xlsx") data_xlsx <- list() ### I then use seq_along in all the files and map_df to read the each excel file for (i in seq_along(files)) { data_xlsx[[i]] <- files[i] %>% excel_sheets() %>% set_names() %>% map_df( ~ read_xlsx(path = files[i], sheet = .x, range = "H3"), .id = "sheet") } # I use the code below to get the files name into the list dat...