简体   繁体   English

R数据导出到带有附加列的Excel

[英]R data export to Excel with additional columns

I'm trying to export data into excel in 5 sections each separated by an empty column between them. 我正在尝试将数据导出到excel中的5个部分,每个部分之间用一个空列分隔。

My data frame has 48 columns. 我的数据框有48列。 I'm failing with last separation where empty column doesn't appear between Section 4 & 5. 我无法进行最后的分隔,在第4节和第5节之间不会出现空列。

My Code: 我的代码:

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)

Could you please indicate where I'm making a mistake? 您能指出我在哪里出错吗?

As mentioned the answer is to indicate the startCol vector in the first argument. 如前所述,答案是在第一个参数中指示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)

Additionally, I highly recommend dividing the whole data frame into sections to improve the readability of the code as well as any future changes to it. 另外,我强烈建议将整个数据框划分为多个部分,以提高代码的可读性以及以后对代码的任何更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM