gpt4 book ai didi

r - 将r中的列表写入excel文件

转载 作者:行者123 更新时间:2023-12-04 21:16:30 24 4
gpt4 key购买 nike

我有一个包含 5 个不同长度的数据帧的列表。我想将这些数据框写在同一张 Excel 表中。

我已经尝试过使用 WriteXLS()write.xlsx()但无法得到想要的结果。

最佳答案

这是一个示例,其中 xlsx包函数用于在同一张表上写 5 个不同的表:

library(xlsx)

### create a sample list
set.seed(123)
theList <- list()

theList$df1 <- data.frame(a=1:5,b=sample(c('X','Y','Z'),5,T))
theList$df2 <- data.frame(a=1:3,b=sample(c('X','Y','Z'),3,T),c=sample(c('A','B','C'),3,T))
theList$df3 <- data.frame(answer=42)
theList$df4 <- data.frame(x=1:2,y=sample(c('I','J','K'),2,T),z='M')
theList$df5 <- data.frame(m=1.2345,n='foo')
###


wb <- createWorkbook()
sheet <- createSheet(wb,"SheetNameHere")

currRow <- 1
for(i in 1:length(theList)){

cs <- CellStyle(wb) + Font(wb, isBold=TRUE) + Border(position=c("BOTTOM", "LEFT", "TOP", "RIGHT"))

addDataFrame(theList[[i]],
sheet=sheet,
startRow=currRow,
row.names=FALSE,
colnamesStyle=cs)

currRow <- currRow + nrow(theList[[i]]) + 2
}

saveWorkbook(wb,file = "myXlsx.xlsx")

结果:

Excel Snapshot

关于r - 将r中的列表写入excel文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27459758/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com