【发布时间】:2020-09-29 08:39:46
【问题描述】:
我有不同的数据框列表,在使用此代码导出到 excel 之前,我将它们绑定在一起:
l_listOfDF <- mget(ls(pattern="listofdf"))
导出工作正常使用:
mywb <- createWorkbook()
for (sheetName in names(listofdfs)){
addWorksheet(mywb , sheetName )
}
# get all lists of tables in a single list
l_listOfDF <- mget(ls(pattern="listofdf"))
# initiate the index of the row where you will want to start writing (one per sheet)
startR <- rep(1, length(listofdfs)) ; names(startR) <- names(listofdfs)
# loop over the lists of tables using index and then over the elements / sheets using their names
for(N_myListOfDF in seq(l_listOfDF)){
for(pageName in names(l_listOfDF[[N_myListOfDF]])){
label_2 <- paste0("Table ", N_myListOfDF, ". Number of ", pageName, " users between 2010-2019")
# write the name/number of your table in the correct sheet, at the correct row
writeData(mywb, sheet=pageName, startRow=startR[pageName], label_2)
# write your data in the correct sheet at the correct row (the one after the name)
writeData(mywb, sheet=pageName, startRow=startR[pageName]+1, l_listOfDF[[N_myListOfDF]][[pageName]])
# update the row number (the + 3 is to leave space for name of table, headers and blank row
startR[pageName] <- startR[pageName]+nrow(l_listOfDF[[N_myListOfDF]][[pageName]]) + 3
}
}
但我无法为要导出的每个表分配正确的标题。
在标签中:
label_2 <- paste0("Table ", N_myListOfDF, ". Number of ", pageName, " users between 2010-2019")
我想添加如下内容:
x <- c("","","females","males")
并获得:
label_2 <- paste0("Table ", N_myListOfDF, ". Number of ", x, pageName, " users between 2010-2019")
为了让每个 excel 表的第一个表具有以下标题:
表 1. 2010-2019 年(pageName)用户数
对于每个excel表的第二个表:
表 2. 2010-2019 年(pageName)用户数
第三个:
表 3. 2010-2019 年女性(页面名称)用户数
第四次:
表 4.男性(页面名称)用户数量(页面名称)2010-2019
但是仅仅在 label_2 中添加向量 x 并不能正常工作。 我在每张表中都增加了表格。
有什么有用的想法吗?谢谢!
这是一个可重现的例子:
listofdfs <- list(data.frame("y"=c(2009,2010,2011),"b"=c(35,30,20)), data.frame("y"=c(2009,2010,2011), "b"=c(6,21,40)), data.frame("y"=c(2009,2010,2011), "a"=c(13,30,5), "b"=c(40,18,25)), data.frame("y"=c(2009,2010,2011), "a"=c(8,36,7), "b"=c(32,9,17)) )
listofdfs_2 <- list(data.frame("y"=c(2009,2010,2011),"b"=c(14,36,8)), data.frame("y"=c(2009,2010,2011), "b"=c(36,27,9)), data.frame("y"=c(2009,2010,2011), "a"=c(9,15,58), "b"=c(7,11,20)), data.frame("y"=c(2009,2010,2011), "a"=c(48,3,67), "b"=c(2,28,37)) )
mywb <- createWorkbook()
for (sheetName in names(listofdfs)){
addWorksheet(mywb , sheetName )
}
# get all lists of tables in a single list
l_listOfDF <- mget(ls(pattern="listofdf"))
# initiate the index of the row where you will want to start writing (one per sheet)
startR <- rep(1, length(listofdfs)) ; names(startR) <- names(listofdfs)
# loop over the lists of tables using index and then over the elements / sheets using their names
for(N_myListOfDF in seq(l_listOfDF)){
for(pageName in names(l_listOfDF[[N_myListOfDF]])){
label_2 <- paste0("Table ", N_myListOfDF, ". Number of ", c("","","females","males"), pageName, " users between 2010-2019")
# write the name/number of your table in the correct sheet, at the correct row
writeData(mywb, sheet=pageName, startRow=startR[pageName], label_2)
# write your data in the correct sheet at the correct row (the one after the name)
writeData(mywb, sheet=pageName, startRow=startR[pageName]+1, l_listOfDF[[N_myListOfDF]][[pageName]])
# update the row number (the + 3 is to leave space for name of table, headers and blank row
startR[pageName] <- startR[pageName]+nrow(l_listOfDF[[N_myListOfDF]][[pageName]]) + 3
}
}
# save your workbook in a file
saveWorkbook(mywb , "diroutput\\pippo.xlsx")
【问题讨论】:
-
有什么想法吗?有一个可重现的例子会更好吗?
-
是的,一个可重现的例子在这里可能会有所帮助。
-
好的,我要编辑我的问题并添加一个示例!
-
完成了!现在好点了吗?
-
下面的答案能满足你的需要吗?