R数据导出到Excel并包含其他列

时间:2018-05-15 08:42:09

标签: r excel dataframe export

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

我的数据框有48列。我在最后分离时失败了,第4节和第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)

请你指出我在哪里弄错了?

1 个答案:

答案 0 :(得分:0)

如前所述,答案是在第一个参数中指出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)

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