Sid
Sid

Reputation: 17

How can we use read_excel for reading .xlsx files in R?

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 :)

Upvotes: 0

Views: 403

Answers (3)

giocomai
giocomai

Reputation: 3518

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)

Upvotes: 2

Diego Rojas
Diego Rojas

Reputation: 199

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)

Upvotes: 0

s_baldur
s_baldur

Reputation: 33488

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

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

Upvotes: 1

Related Questions