1

我创建了一个程序来破坏其他几家银行的数据库。但是在这个程序中,我将这几家银行保存在 txt 中,所以现在我想保存在 xls 中,但我不知道如何。

我试过了for (nm in Nms) write.table(Res[[nm]], paste(nm, 'xls', sep='.'), sep="\t",dec=",",col.names=TRUE, row.names=FALSE, quote=TRUE, na="NA")

但它没有用

decup <- function(dados,var){

require(gdata)

dados <- read.xls("dados.xls")

attach(dados)

Res = split(dados, var)

for (nm in Nms) write.table(Res[[nm]], file=paste(nm, 'txt', sep='.'))

for (nm in Nms) zip(paste(nm,'zip',sep='.'),paste(nm,'xls',sep='.'), zip = Sys.getenv("R_ZIPCMD", "zip"))

}

4

1 回答 1

1

您可以使用库 XLConnect来读取/写入 .xlsx 文件,并 writeWorksheet让我们保存您的 data.frames:

library(XLConnect)
## open workbook or create it if doesn't exist
wb <- loadWorkbook("writeWorksheet.xlsx", create = TRUE)
## for each data.frame create a sheet with its data
lapply(seq_along(Res), function(x)
   createSheet(wb, name = paste0("sheet",x))
   writeWorksheet(wb, Res[[x]], sheet = paste0("sheet",x), startRow = 4, startCol = 2)
 }

# Save workbook (this actually writes the file to disk)
saveWorkbook(wb)

编辑通过工作簿保存 data.frame 你这样做:

lapply(seq_along(Res), function(x){
  wb <- loadWorkbook(paste0("database",x), create = TRUE)
  createSheet(wb, name ='sheet1' )
  writeWorksheet(wb, Res[[x]], sheet = 'sheet1', startRow = 4, startCol = 2)
  saveWorkbook(wb)
})
于 2013-06-23T20:40:00.603 回答