[英]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 时保留它吗?
(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")
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(..))
是因为我们需要一个绝对行号,而不仅仅是每个帧的计数。这导致starts
为L
中的每个帧保留第一个 excel 行号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.