在一个文件夹中,我有许多子文件夹,每个子文件夹包含两个excel文件。我试图将这些文件合并到R中的数据框中。
setwd("wd")
file_list <- list.files("wd")
答案 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)