Mohsen Jafarikia
2016-Jan-13 14:18 UTC
[R] Multiple CSV files in different sheets of an Excel file
I have multiple CSV files that I would like to have them in a single Excel file. For example, I have file1.csv and file2.csv and I want to have file.xls where file1.csv and file2.csv each have been copied to a single sheet of the file.xls file. Thanks, Mohsen [[alternative HTML version deleted]]
Frans Marcelissen
2016-Jan-13 14:44 UTC
[R] Multiple CSV files in different sheets of an Excel file
Hi Mohse, You can do that with the append parameter of the write.xlsx routine in the xlsx package: xlsx::write.xlsx(file1,file='XXXXX.xlsx',sheetName = '1') xlsx::write.xlsx(file2,file='XXXXX.xlsx',sheetName = '2',append = T) Success! Frans 2016-01-13 15:18 GMT+01:00 Mohsen Jafarikia <jafarikia at gmail.com>:> I have multiple CSV files that I would like to have them in a single Excel > file. For example, I have file1.csv and file2.csv and I want to have > file.xls where file1.csv and file2.csv each have been copied to a single > sheet of the file.xls file. > > Thanks, > Mohsen > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Mohsen Jafarikia
2016-Jan-13 14:47 UTC
[R] Multiple CSV files in different sheets of an Excel file
Thanks Frans, My files are CSV. If presume first I should convert them to Excel format and run the code you have suggested. Am I right? Thanks again, Mohsen On Wed, Jan 13, 2016 at 9:44 AM, Frans Marcelissen < fransiepansiekevertje at gmail.com> wrote:> Hi Mohse, > You can do that with the append parameter of the write.xlsx routine in the > xlsx package: > > xlsx::write.xlsx(file1,file='XXXXX.xlsx',sheetName = '1') > xlsx::write.xlsx(file2,file='XXXXX.xlsx',sheetName = '2',append = T) > > Success! > Frans > > 2016-01-13 15:18 GMT+01:00 Mohsen Jafarikia <jafarikia at gmail.com>: > >> I have multiple CSV files that I would like to have them in a single Excel >> file. For example, I have file1.csv and file2.csv and I want to have >> file.xls where file1.csv and file2.csv each have been copied to a single >> sheet of the file.xls file. >> >> Thanks, >> Mohsen >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> > >[[alternative HTML version deleted]]
Marc Schwartz
2016-Jan-13 15:07 UTC
[R] Multiple CSV files in different sheets of an Excel file
> On Jan 13, 2016, at 8:18 AM, Mohsen Jafarikia <jafarikia at gmail.com> wrote: > > I have multiple CSV files that I would like to have them in a single Excel > file. For example, I have file1.csv and file2.csv and I want to have > file.xls where file1.csv and file2.csv each have been copied to a single > sheet of the file.xls file. > > Thanks, > MohsenHi, In general there are several options to create XLS[X] files from within R. Most, like my own WriteXLS package, will require the use of external functionality, typically Perl, Python or Java. WriteXLS requires Perl and there is an installation guide for the package on the CRAN page: cran.r-project.org/web/packages/WriteXLS/index.html next to the Materials header: cran.r-project.org/web/packages/WriteXLS/INSTALL If you elect to go with WriteXLS and satisfy the Perl requirements indicated, you can then use something like the following, after installing the package: require(WriteXLS) CSVFiles <- c("file1.csv", "file2.csv") # Use ?lapply to read in each of the files to a data frame # results in a list of the data frames FILE.List <- lapply(CSVFiles, function(x) read.csv(x)) # write the list of data frames to an Excel file called CSVFiles.xlsx # which will contain 2 worksheets, one per data frame WriteXLS(File.List, "CSVFiles.xlsx") If you have a series of files in a folder to read in, you may also wish to look at ?list.files to fetch a listing of file names by pattern into the CSVFiles vector rather than creating it manually as above. Regards, Marc Schwartz