r:通过将excel文件作为dplyr中的列表传递来编译excel文件

时间:2019-01-16 01:22:12

标签: r excel dplyr openxlsx

我有两个要在r中编译为单个数据帧的excel文件。

首先,创建要复制的Excel文件:

df1<- cbind.data.frame(var1= rnorm(10,4,1), var2= rnorm(10,5,1), var3= rnorm(10,7,4)) # create df1
df2<- cbind.data.frame(var1= rnorm(10,4,1), var2= rnorm(10,5,1), var3= rnorm(10,7,4)) # create df2

wb1<- openxlsx::createWorkbook() # create empty workbook1
wb2<- openxlsx::createWorkbook() # create empty workbook2

openxlsx::addWorksheet(wb1, "df1") # add sheet 1 to wb1
openxlsx::addWorksheet(wb1, "df2") # add sheet 2 to wb1

openxlsx::addWorksheet(wb2, "df1") # add sheet 1 to wb2
openxlsx::addWorksheet(wb2, "df2") # add sheet 2 to wb2

openxlsx::writeData(wb1, "df1", df1) # write df1
openxlsx::writeData(wb1, "df2", df2) # write df2

openxlsx::writeData(wb2, "df1", df1) # write df1
openxlsx::writeData(wb2, "df2", df2) # write df2

openxlsx::saveWorkbook(wb1, 'wb1.xlsx') # save wb1
openxlsx::saveWorkbook(wb2, 'wb2.xlsx') # save wb2

下面的函数将一个指定的Excel文件编译为一个Dataframe,但我想获取所有文件并以编程方式对其进行编译:

dfCompiled <- 'wb1.xlsx' %>% # rename DF and input your file name here
  getSheetNames() %>%
  set_names() %>%
  map(read.xlsx, xlsxFile = 'wb1.xlsx', # file name here
      colNames = TRUE) %>% 
  as.data.frame() 

调用数据框以验证其是否有效:

> dfCompiled
    df1.var1 df1.var2    df1.var3 df2.var1 df2.var2   df2.var3
1  3.356598 4.441104  7.95931350 3.968744 3.349242  2.1997116
2  3.151004 4.822166  0.39571905 4.679021 6.230923 12.8589661
3  3.581085 6.367498 -0.06415929 5.810634 4.207270  9.9430692
...

通过这些语句运行以下列表的最佳方法是什么,以便将所有工作表都编译到一个数据框中?

filelist<- list("wb1.xlsx", "wb2.xlsx" )

1 个答案:

答案 0 :(得分:2)

类似:

library(tidyverse)

df_compile <- function(file){
  getSheetNames(file) %>%
  set_names() %>%
  map(read.xlsx, xlsxFile = file,
      colNames = TRUE) %>% 
  as.data.frame() 
}

filelist<- list("wb1.xlsx", "wb2.xlsx")

# Assuming same column names per xlsx file
map(filelist, df_compile) %>%
map_df(bind_rows)