R中的函数保存在* .xls中

时间:2013-06-23 20:33:37

标签: r function xls

我创建了一个破坏其他几家银行数据库的程序。但是在这个程序中我将这几个银行保存在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"))

}

2 个答案:

答案 0 :(得分: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)
})

答案 1 :(得分:0)

确实你是对的,试试包xlsx

此链接应该有帮助

http://danganothererror.wordpress.com/2012/02/12/write-data-frame-to-excel-file/