0

I want to read all the Excel files one by one to temp.data variable. Is there a way to do it? I tried this

for (i in 1:15) { temp.data <- read_excel(file.path("../source",files$file_path[[i]])) } 

Or is there an alternative way for NOT using for loop here?

Thanks in advance :)

1
  • You store all of them in the same variable, so override previous file with new file. You should store the data in a table Commented Jan 22, 2020 at 13:37

3 Answers 3

2

If you want the ouptut in a data frame and you have relevant files in the "source" folder, the following should do:

purrr::map_dfr(list.files("../source", full.names = TRUE), read_excel) 

If you have only specific files included in a vector as it appears from your code, this may serve you well:

purrr::map_dfr(file.path("../source",files$file_path), read_excel) 
Sign up to request clarification or add additional context in comments.

2 Comments

where should i keep files$file_path[[i]]) in purrr::map_dfr(list.files("../source", full.names = TRUE), read_excel) @giocomai
that line of code assumes you have all files in that "source" folder... if you have a list of files in a vector, then you can put that vector instead of list.files("../source", full.names = TRUE) (without the [i] that comes from the loop... there's no loop here)
1

To store the resulting data.frames in a list you can do:

lapply(file.path("../source", files$file_path), read_excel) 

Comments

0

you can use de map function from the purrr package:

your_path = "C:\...\2_souce_data" files_xlsx <- list.files(your_path, pattern = ".xlsx", full.names = T) temp.data = map(files_xlsx, read_excel, sheet = "your_sheet_index_or_name", col_names =TRUE) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.