当我在循环中使用 write.xlsx 时,如何处理空数据帧?
下面是循环的样子,其中 source("./Scripts/Analysis_details.R") 引用了创建数据框的 r 文件。
library(xlsx)
for (A in unique(df_base$A)) {
df<- df_base[df_base$A==A,]
source("./Scripts/Analysis_details.R")
output_file = paste("./Output/report_", A, '_', Sys.Date(), ".xlsx", sep='')
write.xlsx(df1, file=output_file, sheetName="df1", append=TRUE, row.names=FALSE, showNA = FALSE)
write.xlsx(df2, file=output_file, sheetName="df2", append=TRUE, row.names=FALSE, showNA = FALSE)
write.xlsx(df3, file=output_file, sheetName="df3", append=TRUE, row.names=FALSE, showNA = FALSE)
write.xlsx(df4, file=output_file, sheetName="df4", append=TRUE, row.names=FALSE, showNA = FALSE)}
我得到的错误是......
Error in mapply(setCellValue, cells[seq_len(nrow(cells)), colIndex[ic]], : zero-length inputs cannot be mixed with those of non-zero length