【问题标题】:R data export to Excel with additional columnsR 数据导出到带有附加列的 Excel
【发布时间】:2018-05-15 08:42:08
【问题描述】:

我正在尝试将数据导出到 5 个部分中的 excel 中,每个部分由它们之间的空列分隔。

我的数据框有 48 列。我在第 4 节和第 5 节之间没有出现空列的最后一次分离失败了。

我的代码:

Section1     <- DataFrame[,1:7]
Section2     <- DataFrame[,8:12]
Section3     <- DataFrame[,13:30]
Section4     <- DataFrame[,31:35]
Section5     <- DataFrame[,36:48]

writeData(wb, sheet = 1, Section1, rowNames = FALSE, colNames = TRUE, keepNA = FALSE)
writeData(wb, sheet = 1, Section2, rowNames = FALSE, colNames = TRUE, keepNA = FALSE, startCol = 9)
writeData(wb, sheet = 1, Section3, rowNames = FALSE, colNames = TRUE, keepNA = FALSE, startCol = 15)
writeData(wb, sheet = 1, Section4, rowNames = FALSE, colNames = TRUE, keepNA = FALSE, startCol = 34)
writeData(wb, sheet = 1, Section5, rowNames = FALSE, colNames = TRUE, keepNA = FALSE, startCol = 39)

saveWorkbook(wb, "/View.xlsx", overwrite = TRUE)

您能否指出我在哪里犯了错误?

【问题讨论】:

  • 您好 Jakub,欢迎来到 SE。您能否在您的问题中添加一些示例数据?提供一个可重复的例子会大大增加你得到正确和完整答案的机会。话虽如此,如果我对您的问题的理解是正确的,我会从第 40 列而不是第 39 列开始:writeData(wb, sheet = 1, Section5, rowNames = FALSE, colNames = TRUE, keepNA = FALSE, startCol = 40)
  • @byouness,您好,感谢您的热情欢迎。不幸的是,我现在无法提供数据示例。就您而言 - 我已经尝试过了,但第 39 列的填充值与第 40 列相同。
  • @byouness,问题已解决。我为 Section1 提供了一个 startColumn 变量,它开始工作了。感谢支持!
  • 太棒了。请用您的所作所为发布您的问题的答案,以便其他人可以从中受益。谢谢。

标签: r excel dataframe export


【解决方案1】:

如前所述,答案是在第一个参数中指明 startCol 向量。

Section1     <- DataFrame[,1:7]
Section2     <- DataFrame[,8:12]
Section3     <- DataFrame[,13:30]
Section4     <- DataFrame[,31:35]
Section5     <- DataFrame[,36:48]

writeData(wb, sheet = 1, Section1, rowNames = FALSE, colNames = TRUE, keepNA = FALSE, startCol = 1)
writeData(wb, sheet = 1, Section2, rowNames = FALSE, colNames = TRUE, keepNA = FALSE, startCol = 9)
writeData(wb, sheet = 1, Section3, rowNames = FALSE, colNames = TRUE, keepNA = FALSE, startCol = 15)
writeData(wb, sheet = 1, Section4, rowNames = FALSE, colNames = TRUE, keepNA = FALSE, startCol = 34)
writeData(wb, sheet = 1, Section5, rowNames = FALSE, colNames = TRUE, keepNA = FALSE, startCol = 39)

此外,我强烈建议将整个数据框分成多个部分,以提高代码的可读性以及将来对其进行的任何更改。

【讨论】:

    猜你喜欢
    • 2018-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-30
    • 2013-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多