0

I have multiple xlsx data files with different number of rows. How can I merge them into a data frame? My code

library(readxl)

fnames <- list.files()

xlxs <- lapply(fnames, read_xlsx)

a<-data.frame(xlxs)

But the last line does not work since number of rows are not same for all files.

alistaire
  • 40,464
  • 4
  • 71
  • 108
ash1
  • 107
  • 1
  • 6

2 Answers2

0

We can use

library(dplyr)
a <- bind_rows(xlxs)

Also, it is better to specify the pattern

fnames <- list.files(pattern = "\\.xlsx", full.names = TRUE)
akrun
  • 789,025
  • 32
  • 460
  • 575
  • 1
    I often also like to have the input filenames referenced in the final data frame. You can use `xlsx – rosscova May 28 '21 at 00:46
0

The purrr option from @alistaire are likely better options, but in case anyone needs/wants to do this in base, you can do:

a <- do.call(rbind, xlsx)
akrun
  • 789,025
  • 32
  • 460
  • 575
rosscova
  • 5,150
  • 1
  • 17
  • 33