简体   繁体   English

将不同的数据框导出到同一个 Excel 工作表

[英]Export different dataframes to the same Excel sheet

I am using openxlsx package to export data frames from R to excel.我正在使用openxlsx包将数据帧从 R 导出到 excel。

How can I export several dataframes:如何导出多个数据帧:

a <- data.frame("y"=c(2009,2010,2011,2012),"b"=c(3,4,5,6))
b <- data.frame("y"=c(2009,2010,2011,2012),"b"=c(12,2,7,8))
c <- data.frame("y"=c(2009,2010,2011,2012),"b"=c(5,9,1,6))

on the same excel sheet, each one separated from the previous one by an empty row, and add a column names of each one?在同一张excel表上,每一个都与前一个空行隔开,并添加每个的列名?

We can use writeData playing with startRow argument, here an example:我们可以使用带有startRow参数的writeData播放,这里是一个例子:

# create workbook
wb <- createWorkbook()
addWorksheet(wb, "Sheet1")

# add dataframes a,b,c starting on different rows
writeData(wb = wb, sheet = "Sheet1", startCol = 1,
               startRow = 1, x = a)
writeData(wb = wb, sheet = "Sheet1", startCol = 1,
               startRow = 1 + nrow(a) + 2, x = b)
writeData(wb = wb, sheet = "Sheet1", startCol = 1,
               startRow = 1 + nrow(a) + 2 + nrow(b) + 2, x = c)

# outout to a file
saveWorkbook(wb, "myFile.xlsx", overwrite = TRUE)

在此处输入图片说明

Maybe you can try something like below也许你可以尝试像下面这样

df <- do.call(rbind,lapply(list(a,b,c),function(x) rbind(x,"")))
write.xlsx(df,"xxx.xlsx")

where df looks like df样子

> df
      y  b
1  2009  3
2  2010  4
3  2011  5
4  2012  6
5
6  2009 12
7  2010  2
8  2011  7
9  2012  8
10
11 2009  5
12 2010  9
13 2011  1
14 2012  6
15

If you want to vertically stack those data frames, here is another version (similar to the one by @zx8754 )如果你想垂直堆叠这些数据框,这是另一个版本(类似于@zx8754 的版本

wb <- createWorkbook()
addWorksheet(wb, "Sheet1")
lst <- list(a,b,c)
startRows <- cumsum(c(1,(sapply(lst,nrow)+2)[-length(lst)]))
for (k in seq_along(lst)) {
  writeData(wb = wb, sheet = "Sheet1", startCol = 1, startRow = startRows[k], x = lst[[k]])
}
saveWorkbook(wb, "xxxx.xlsx", overwrite = TRUE)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R Shiny将两个数据框导出到同一Excel工作表中的两个工作表中 - R Shiny export two dataframes into two sheets in same excel sheet 使用r将多个数据帧导出到一个Excel工作表 - Export multiple dataframes to one Excel Sheet with r 从 Rstudio 中的同一 excel 表导入两个数据帧作为单独的数据帧 - Import two dataframes from the same excel sheet in Rstudio as separated dataframes 循环将 r 中的多个数据框导出到 Excel 并命名每个工作表 - Loop to export multiple dataframes in r to Excel and Name Each Sheet 使用R在同一Excel工作表中优雅输出几个数据框 - Elegantly output several dataframes in the same Excel sheet with R R:将几个Excel文件读入不同的数据框并同时进行浏览 - R: Reading several Excel files into different dataframes and exploring these at the same time 将 R 3 个数据帧导出到同一 excel 工作簿中的 3 个不同工作表 - Exporting R 3 dataframes to 3 different sheets in the same excel workbook 如何将同一数据源中的不同组导出到不同的 Excel 工作簿? - How to export different groups in same data source to different Excel workbooks? 将列表中的数据帧导出为Excel文件中的工作表 - Export dataframes in list as worksheets in Excel file 读取同一目录中的所有 Excel 工作簿,每个工作簿都有多个工作表,并将每个工作表导出为 R 中的 a.csv - Read all Excel workbooks in the same directory each with multiple sheets and export each sheet as a .csv in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM