-5

I have 4 excel files in the one folder on my computer. The files contain both alphanumeric and numeric data. I want to write a function to sequentially read each xlsx file, convert it to a dataframe, then store the dataframe names in a list. This will allow me to use "lapply" with a function on each of these dataframes later on.

Any ideas?

Jim Chen
  • 3,262
  • 3
  • 21
  • 35
  • `purrr` is a great tool to make this simple. https://readxl.tidyverse.org/articles/articles/readxl-workflows.html – Jon Spring Sep 27 '18 at 22:22
  • Possible duplicate of [Using lapply and read.csv on multiple files (in R)](https://stackoverflow.com/questions/13441204/using-lapply-and-read-csv-on-multiple-files-in-r) – Jim Chen Sep 28 '18 at 02:14
  • Possible duplicate of [read.csv from list to get unique colnames](https://stackoverflow.com/questions/49562784/read-csv-from-list-to-get-unique-colnames) – Anonymous coward Sep 28 '18 at 14:20

1 Answers1

0

list.files() lists all the files in a directory, use this with lapply() and read_xlsx(). In this example I set the working directory to the folder containing the files.

library(readxl)
files <- list.files()
dat <- lapply(files, read_xlsx)
dat

# [[1]]
# # A tibble: 1 x 3
#       a  a__1  a__2
#   <dbl> <dbl> <dbl>
# 1     1     1     1
# 
# [[2]]
# # A tibble: 1 x 3
#       b  b__1  b__2
#   <dbl> <dbl> <dbl>
# 1     2     2     2
# 
# [[3]]
# # A tibble: 1 x 3
#       c  c__1  c__2
#   <dbl> <dbl> <dbl>
# 1     3     3     3
Paul
  • 2,877
  • 1
  • 12
  • 28