I am looping to load multiple xlsx files. This I am doing well. But when I want to add the name of the columns of the documents (the same names for all files) I have not managed to do it.
library(dplyr)
library(readr)
library(openxlsx)
library(readxl)
setwd("C:/Users/MiguelAngel/Documents/R Miguelo/Guillermo Ahumada")
ldf <- list()
listxlsx <- dir(pattern = "*.xlsx")
for (k in 1:length(listxlsx)){
ldf[[k]] <-as.data.frame(read.xlsx(listxlsx[k]))
}
The result: 355 1500 1100 43831 1 190 850 600 43832 2 93 4000 3000 43833 3 114 4000 3000 43834 4 431 1000 700 43835 5 182 1000 700 43836 6 496 500 300 43837 7 254 500 300 43838 8 174 600 300 43839 9 397 1500 945 43840 10 198 1500 900 43841 11 271 1500 900 43842 12 94 3000 2000 43843 13 206 400 230 43844 14 305 1500 1100 43845 15 184 850 600 43846 16 90 4000 3000 43847 17 70 4000 3000 43848 18 492 1000 700 43849 19 168 1000 700 43850 20 530 500 300 43851
They load all the files well but without the name of the columns.
I need add the name of columns:
list_file <- dir(pattern = "*.xlsx") %>%
lapply(read.xlsx) %>% # *I use stringAsFactor but appear error.
bind_rows
but appear this list_file
Form of original columns all files
I need put this columns names after make the loop with for.
Thanks for help me guys
I cannot check this since I don't have Excel files to load, but I think this should work:
listxlsx <- list.files(path = "C:/Users/MiguelAngel/Documents/R Miguelo/Guillermo Ahumada", pattern = "*.xlsx", full.nams = TRUE)
names(listxlsx) <- listxlsx
purrr::map_dfr(listxlsx, readxl::read_excel, .id = "Filename")
(The first line is a better practice to get the filenames than relying on setwd
.)
When listxlsx
is a named vector the function map_dfr
gives a column named Filename
where the values are taken from listxlsx
.