Try this:
list_of_excel_files <- list.files(pattern = ".xlsx")
get_cols <- function(file) {
x <- readxl::read_excel(file)
cols <- colnames(x)
cols
}
purrr::map(list_of_excel_files, get_cols)
# If you wanted a named list
purrr::map(list_of_excel_files, get_cols) %>%
setNames(list_of_excel_files)
The map() function in the purrr package replaces the for loops for cleaner code. I set up a function, get_cols() which works for a single data set, and then I use map() to iterate over the list of excel files and apply it to each data set.