英文:
Format dataframe to Excel using openxlsx
问题
以下是您要翻译的内容:
"Any suggestion how can i deal with the two first rows? My data frame actually is just the second row (which has the title of the columns) to the bottom. But i don't have any idea how to add the first line
I've tried to pack two data frames:
addWorksheet(wb, "sheet2")
auxdata = tibble(TEST = rep('',nrow(base_original)))
writeData(wb, sheet = 3, x = auxdata)
writeData(wb, sheet = 3, x = df1)
but didn't work of course"
英文:
Any suggestion how can i deal with the two first rows? My data frame actually is just the second row (which has the title of the columns) to the bottom. But i don't have any idea how to add the first line
I've tried to pack two data frames:
addWorksheet(wb, "sheet2")
auxdata = tibble(`TEST` = rep('',nrow(base_original)))
writeData(wb, sheet = 3, x = auxdata)
writeData(wb, sheet = 3, x = df1)
but didn't work of course
答案1
得分: 0
writeData
有一个startRow
(和startCol
)参数来指定要写入数据的区域。因此,您可以设置startRow=2
来写入您的数据集。然后在第一行添加额外的标题行:
library(openxlsx)
fn <- tempfile(".xlsx")
df1 <- mtcars[, 1:2]
wb <- createWorkbook()
addWorksheet(wb, "sheet2")
writeData(wb, sheet = "sheet2", x = "TEST")
writeData(wb, sheet = "sheet2", x = df1, startRow = 2)
mergeCells(wb, sheet = "sheet2", cols = 1:2, rows = 1)
saveWorkbook(wb, fn)
英文:
writeData
has a startRow
(and startCol
) argument to specify the region to write the data. Hence, you can set startRow=2
to write your dataset. Then add the additional header line in the first row:
library(openxlsx)
fn <- tempfile(".xlsx")
df1 <- mtcars[, 1:2]
wb <- createWorkbook()
addWorksheet(wb, "sheet2")
writeData(wb, sheet = "sheet2", x = "TEST")
writeData(wb, sheet = "sheet2", x = df1, startRow = 2)
mergeCells(wb, sheet = "sheet2", cols = 1:2, rows = 1)
saveWorkbook(wb, fn)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论