Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save Excel spreadsheet as .csv with R?

Tags:

r

excel

What is the simplest way to convert a large Excel spreadsheet with multiple worksheets into .CSV files in R?

Note that I've tested XLConnect and XLSX and found that my Excel sheets cause it to crash. So I'm specifically looking for a solution that does not use the XLConnect or XLSX package.

like image 301
AME Avatar asked Nov 18 '11 20:11

AME


2 Answers

Here's a loop to write out all sheets:

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)
}
like image 88
JD Long Avatar answered Oct 25 '22 11:10

JD Long


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

EDIT: to address the read.xlsx option:

If you have Perl running, you need a current version of 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)

This was done on a Mac and up until this question I had always stumbled at the installXLSXsupport() stage since I always got an error. This time I started up Perl from a Terminal command line, and got success after first setting up my personal configuration, defining CPAN mirrors on my continent, and I left perl running.

like image 5
IRTFM Avatar answered Oct 25 '22 12:10

IRTFM