简体   繁体   English

编写一个 excel 或 csv 文件,使数据帧列在同一张纸上,而不是 R 中的多张纸

[英]Write an excel or csv file in a way that the dataframes are listed on the same sheet, instead of multiple sheets in R

I have an object that contains multiple dataframes and wanted to produce a single excel worksheet with the data.我有一个包含多个数据框的 object,我想生成一个包含数据的 excel 工作表。 I know there are ways of dealing with this problem in excel. But is there a way to manipulate it from the R side, so that people don't have to worry about the extra steps that weren't in my R script?我知道在 excel 中有处理这个问题的方法。但是有没有办法从 R 端来处理它,这样人们就不必担心我的 R 脚本中没有的额外步骤? I have been using this function (see below), but am open to another function. This function produces 1 excel file, but a worksheet for every dataframe. I have 119 dataframes.我一直在使用这个 function(见下文),但我对另一个 function 开放。这个 function 产生 1 个 excel 文件,但每个 dataframe 都有一个工作表。我有 119 个数据帧。 So this is not really practical.所以这不是很实用。

write_xlsx(results1, "hpresponse1.MinimallyAdjusted") 

I used the bind_rows.我使用了 bind_rows。 However, some of the data was lost.但是,一些数据丢失了。 I am not sure how to retain it, especially as I don't even know what kind of data it is.我不确定如何保留它,尤其是因为我什至不知道它是什么类型的数据。 But I turned my results for logistic regression into a dataframe so that I was able to perform certain manipulations.但我将逻辑回归的结果转换为 dataframe,以便我能够执行某些操作。 There are labels of some kind off to the left that are not variables.左边有一些不是变量的标签。 Can I turn these data into a variable so that it is retained when I use bind_rows?我可以把这些数据变成一个变量,以便在我使用 bind_rows 时保留它吗?

我的数据框 1 的示例

(Up front, I'm assuming that the 199 frames are all different . Or at least that they are structured or used such that you must not combine them into a single frame, as stefan has suggested in the comments.) (在前面,我假设 199 个帧都是不同的。或者至少它们的结构或使用方式使得您不能将它们组合成一个帧,正如 stefan 在评论中所建议的那样。)

I suggest you use the openxlsx package and offset each table individually.我建议您使用openxlsx package 并分别偏移每个表。

L <- list(mtcars[1:3,1:3], mtcars[4:5,1:5], mtcars[6:10,1:4])
wb <- openxlsx::createWorkbook("quux.xlsx")
sapply(L, nrow)
# [1] 3 2 5
starts <- cumsum(c(1L, 2L + sapply(L[-length(L)], nrow)))
starts
# [1]  1  6 10

wb <- openxlsx::createWorkbook()
openxlsx::addWorksheet(wb, "quuxsheet")
ign <- Map(function(x, st) openxlsx::writeData(wb, sheet = "quuxsheet", x = x, startRow = st), L, starts)
openxlsx::saveWorkbook(wb, file = "quux.xlsx")

包含三个表格的 excel 工作表,间隔

  • sapply(L, nrow) gives us the number of rows in each table in the list. sapply(L, nrow)为我们提供了列表中每个表的行数。 This is used so that we know to offset so-many-rows after a table for the next table.这是为了让我们知道在一个表之后为下一个表偏移这么多行。 Since we don't care about the number of rows in the last frame, we omit it with L[-length(L)]因为我们不关心最后一帧的行数,所以我们用L[-length(L)]省略它
  • 2L + sapply(..) gives us a gap of 1 row between each frame in the worksheet. 2L + sapply(..)使我们在工作表中的每个帧之间有 1 行的间隙。 Change to to suit your needs.更改为以满足您的需要。
  • cumsum(c(1L, 2L+sapply(..)) is because we need an absolute row number, not just the counts for each frame. This results in starts holding the first excel row number for each frame in L . cumsum(c(1L, 2L+sapply(..))是因为我们需要一个绝对行号,而不仅仅是每个帧的计数。这导致startsL中的每个帧保留第一个 excel 行号。

暂无
暂无

声明:本站的技术帖子网页,遵循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? - how to loop through multiple dataframes and write into multiple sheets of excel in R? 读取同一目录中的所有 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 在 shiny 应用程序的同一 excel 文件的多个工作表中下载多个数据帧 - Download multiple dataframes in multiple sheets of the same excel file in a shiny app 如何在R中的一个csv excel文件的多个表中写入多个数据帧? - how to write multiple dataframe to multiple sheet of one csv excel file in R? 使用r将多个数据帧导出到一个Excel工作表 - Export multiple dataframes to one Excel Sheet with r R 使用值而不是公式导入 Excel 文件(多页) - R Import Excel file with values instead of formulas (multiple sheets) 将 R 3 个数据帧导出到同一 excel 工作簿中的 3 个不同工作表 - Exporting R 3 dataframes to 3 different sheets in the same excel workbook 使用R在同一Excel工作表中优雅输出几个数据框 - Elegantly output several dataframes in the same Excel sheet with R 在 R 中将 a.CSV 文件添加到 excel 作为工作表 - Adding a .CSV file to excel as a sheet in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM