无法覆盖R数据框到现有Excel中

时间:2017-10-25 16:57:38

标签: r excel dataframe insert append

我是初学者,我试图简单地将RS数据框从RStudio插入到现有的Excel工作表中,而不会丢失该工作表中的先前数据或覆盖整个文件,例如。在文件" Reporting.xlsx"中插入新数据框(15行,4列)在Sheet" August"从第16行第1列开始,以便之前的信息不会丢失?

谢谢!

2 个答案:

答案 0 :(得分:3)

这样的事情:

library(openxlsx)
library(dplyr)

# Get existing data and append new data
dat = readWorkbook("Reporting.xlsx", sheet="August")
dat = bind_rows(dat, new_data_frame)

# Write updated data frame to existing worksheet
wb = loadWorkbook("Reporting.xlsx")
writeData(wb, "August", dat)

# Save file (with new name for testing purposes)
saveWorkbook(wb, "Reporting_test.xlsx")

# To overwrite the pre-existing data file, you can do the following (commented out for safety)
#saveWorkbook(wb, "Reporting.xlsx", overwrite=TRUE)

答案 1 :(得分:2)

我建议将Excel文件导入R Studio,通过合并R数据框和Excel数据框创建新的数据框,然后导出文件。