将子文件夹中的excel文件组合到R

时间:2019-02-05 00:19:27

标签: r excel

在一个文件夹中,我有许多子文件夹,每个子文件夹包含两个excel文件。我试图将这些文件合并到R中的数据框中。

setwd("wd")
file_list <- list.files("wd")

2 个答案:

答案 0 :(得分:0)

合理的起点:

files <- list.files(pattern = ".*\\.xlsx$", path = "wd", recursive = TRUE, full.names = TRUE)
lst_of_frames <- lapply(files, readxl::read_excel)

# assuming *all* files have exactly the same structure
oneframe <- do.call("rbind.data.frame", lst_of_frames)
# if there are some *minor* differences between them, then one of the following might work better
oneframe <- dplyr::bind_rows(lst_of_frames)
oneframe <- data.table::rbindlist(lst_of_frames, fill = TRUE)

答案 1 :(得分:-1)

我知道它可以与以下代码一起使用。

files <- list.files(pattern = "*.xls", path = "wd", recursive = TRUE, full.names = TRUE, all.files = TRUE, include.dirs = TRUE)

lst_of_frames <- lapply(files, readxl::read_excel)

oneframe <- do.call("rbind.data.frame", lst_of_frames)