使用R将Excel电子表格另存为.csv?

AME*_*AME 8 excel r

将包含多个工作表的大型Excel电子表格转换为R中的.CSV文件的最简单方法是什么?

请注意,我已经测试了XLConnect和XLSX,发现我的Excel工作表导致它崩溃.所以我特意寻找不使用XLConnect或XLSX软件包的解决方案.

JD *_*ong 9

这是一个写出所有表格的循环:

require(gdata)
## install support for xlsx files
installXLSXsupport()
excelFile <- ("/full/path/to/excelFile.xlsx")
## note that the perl scripts that gdata uses do not cope well will tilde expansion
## on *nix machines. So use the full path. 
numSheets <- sheetCount(excelFile, verbose=TRUE)

for ( i in 1:numSheets) {
  mySheet <- read.xls(excelFile, sheet=i)
  write.csv(mySheet, file=paste(i, "csv", sep="."), row.names=FALSE)
}
Run Code Online (Sandbox Code Playgroud)


42-*_*42- 5

http://rwiki.sciviews.org/doku.php?id=tips:data-io:ms_windows

编辑:解决read.xlsx选项:

如果您运行Perl,则需要当前版本的gdata

require(gdata)
installXLSXsupport()   #now the example from help(read.xls)
    # load the third worksheet, skipping the first two non-data lines...
    if( 'XLSX' %in% xlsFormats() )  # if XLSX is supported..
      data <- read.xls(exampleFile2007, sheet="Sheet with initial text", skip=2)
 data
#-----------------------
   X       X.1 D E.  F  G Factor
1 NA  FirstRow 1 NA NA NA   Red 
2 NA SecondRow 2  1 NA NA Green 
3 NA  ThirdRow 3  2  1 NA   Red 
4 NA FourthRow 4  3  2  1 Black 
#------------------------
write.csv(data)
Run Code Online (Sandbox Code Playgroud)

这是在Mac上完成的直到这个问题我总是偶然发现在installXLSXsupport()阶段,因为我总是遇到错误.这次我从终端命令行启动了Perl,并在首次设置我的个人配置,在我的大陆上定义CPAN镜像后得到了成功,并且我让perl运行了.