-1

I currently have 12 excel files that are of the same length that I would like to read in R and combine them into one data frame for analysis, how do I do this? I would prefer to not have to go through and name them once the data set gets bigger and just be able to merge all files in one folder into one workbook/dataframe.

This is what they look like at the moment:

xyzP1<-data.frame(read_excel("P1xyz.xlsx"))
xyzP2<-data.frame(read_excel("P2xyz.xlsx"))
xyzP3<-data.frame(read_excel("P3xyz.xlsx"))
xyzL1<-data.frame(read_excel("L1xyz.xlsx"))
xyzL2<-data.frame(read_excel("L2xyz.xlsx"))
xyzL3<-data.frame(read_excel("L3xyz.xlsx"))
xyzF1<-data.frame(read_excel("F1xyz.xlsx"))
xyzF2<-data.frame(read_excel("F2xyz.xlsx"))
xyzF3<-data.frame(read_excel("F3xyz.xlsx"))
xyzB1<-data.frame(read_excel("B1xyz.xlsx"))
xyzB2<-data.frame(read_excel("B2xyz.xlsx"))
xyzB3<-data.frame(read_excel("B3xyz.xlsx"))

Thanks!

  • Try `Reduce(function(...) merge(...), mget(ls(pattern = "^xyz")))` – akrun Jun 16 '22 at 16:31
  • Hi. What does "merge" mean to you in this case? Do you want to read the files and stack them? Or something more involved? –  Jun 16 '22 at 17:12
  • Please check https://stackoverflow.com/questions/32888757/how-can-i-read-multiple-excel-files-into-r – CelloRibeiro Jun 16 '22 at 17:55
  • Does this answer your question? [How to read in multiple ".xlsx" files to R](https://stackoverflow.com/questions/33771402/how-to-read-in-multiple-xlsx-files-to-r) – Andrea M Jun 16 '22 at 21:52

1 Answers1

0

If the excel files are all the same you can perform a function such as

setwd("./your_folder_path") 
df = list.files(all.files = T,  pattern = ".csv", full.names = F, recursive = TRUE) 

read_excels = function(path) {
      ( df2 = read.delim (path, header = FALSE, sep = ";",

                                                  } 
combination_excels = map_dfr(df, read_excels)
CelloRibeiro
  • 160
  • 11