如何使for循环命令rbind同一目录中的所有excel文件

时间:2019-05-27 01:16:09

标签: r for-loop

我想整理目录中的所有excel文件(无论文件总数如何),并在新列中填充文件名,这样我就可以确定数据来自何处。例如,我有这样的文件模板

promo  stock
  a     200
  b     200
  c     200

要这样

store  promo  stock
file1    a     200
file1    b     200
file1    c     200
file2    a     200
file2    b     200
file2    c     200
file3    a     200
file3    b     200
file3    c     200

如何针对这种情况下进行for循环订购?提前谢谢

2 个答案:

答案 0 :(得分:2)

尽管Reduce(rbind)可能会变慢,具体取决于您拥有的文件数量,但这应该可以做到。另外,您可能需要修改代码以基于扩展名读取文件。让我知道-

# set working directory to where the files are using setwd()

result <- lapply(dir(), function(x) {
  cbind(store = x, read.csv(paste0(x, ".csv"), header = T, stringsAsFactors = F))
}) %>% 
  {Reduce(rbind, .)}

答案 1 :(得分:2)

我们可以找到文件的完整路径名,然后使用lapplyreadxl::read_excel读取每个文件,并添加一个新列store并添加文件名

file_paths <- list.files("/path/to/files", pattern = ".xlsx$", full.names = TRUE)

do.call(rbind, lapply(file_paths, function(x) 
    transform(readxl::read_excel(x), store = sub(".xlsx$", "", basename(x)))))

#  promo stock store                                            
#1     a   200 file1
#2     b   200 file1
#3     c   200 file1
#4     a   200 file2
#5     b   200 file2
#6     c   200 file2

我们也可以使用purrr::map_df

purrr::map_df(file_paths, ~transform(readxl::read_excel(.), 
                            store = sub(".xlsx$", "", basename(.))))