-2

I need to extract data (date, time) from two cells (E6:E7) from multiple excel files and then store that data into a data frame (with separate columns for date and time).

See below for the code I've tried.

setwd("C:/Users/tsamuels/Desktop/PDRA - Collins/growth_rate_folder")
file.list <- list.files(path=".", pattern="\\d.xlsx$")
df.list <- lapply(file.list, read_excel(path=".", sheet = 1, range = "E6:E7", col_names = FALSE,
                                        col_types = NULL))

Any help appreciated!

Ben
  • 28,684
  • 5
  • 23
  • 45

2 Answers2

0

I am not using read_excel and you did not provide a MRE so I could not test it, but you could try this.

df.list <- lapply(file.list, read_excel, sheet=1, range="E6:E7", col_names=FALSE, col_types = NULL))
dario
  • 6,415
  • 2
  • 12
  • 26
  • That partially worked! I've now got a tibble for the data from each excel file, how would I get all of that data into a single tibble? – Toby Samuels Feb 09 '20 at 22:37
  • [this should help](https://stackoverflow.com/questions/8091303/simultaneously-merge-multiple-data-frames-in-a-list) or [this (row bind)](https://www.rdocumentation.org/packages/data.table/versions/1.12.8/topics/rbindlist) – dario Feb 09 '20 at 22:44
0

Making some assumptions about your files and how you want your output:

files <- list.files('.', pattern = '*.xlsx', recursive = TRUE)
df1 <- lapply(files, 
              function(x) {readxl::read_xlsx(x, sheet = 1, range = "E6:E7", col_names = FALSE)}) %>% 
  bind_cols %>% 
  t %>% 
  data.frame

colnames(df1) <- c('date', 'time')
rownames(df1) <- files

Here bind_cols (or do you need bind_rows?) will take your list of dataframes and combine them

userABC123
  • 1,460
  • 2
  • 18
  • 31