I am trying to extract the column names of 20 datasets in my directory. Not sure what wrong i am doing here. Can anyone help me
## list the excel file
list_of_excel_files <- list.files(pattern = ".xlsx")
## read all excel file in a single command
for (i in 1:length(list_of_excel_files)) {
# print(list_of_excel_files[1])
filenames <- list_of_excel_files[i]
filenames_data <- read_excel(filenames, skip = 1)
assign(x = filenames, value = filenames_data)
}
## read column names for the above datasets
for (i in 1:length(ls(pattern = "xlsx"))) {
col_names <- ls(pattern = "xlsx")[i]
#print(col_names)
display_colnames <- colnames(col_names)
assign(x = paste0(col_names,"_colnames"), value = display_colnames)
}
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.