[R] Appending many different and separate Excel files using R

Rui Barradas ruipbarradas at sapo.pt
Tue Sep 18 15:52:28 CEST 2012


Hello,

This is the sort of question that could interess others, so you should 
have CCed it to the list.
As for the question, from the package vignette, section 3.3.6:

"writeWorksheetToFile() is a wrapper function, calling loadWorkbook(), 
createSheet() and saveWorkbook()
functions subsequently. It therefore allows for writing data into 
worksheets of an Excel file in one call."

To see it, run
vignette('XLConnect', 'XLConnect')

Since it seems that you don't need several worksheets in the output 
file, a work around would be to use write.csv, like you do in your post. 
With the side effect that any other software package would be able to 
access, and get rid of Excel dependency.
But if for some reason you need Excell, then the error message is clear, 
only file extensions xls and xlsx are allowed.

Rui Barradas

Em 18-09-2012 13:54, Amir Kasaeian escreveu:
>
>
>
> Hello,
> Thank you for all your support and help.
> It works though I have a new problem and I don’t know can I ask you my questions.
> For example, I encountered the below message when I append 30 files each of size 1000 observations with about 100 variables which differ in ten of them.
> After performing the final line of the R codes I encountered the below message I don’t know how to resolve it.
> Thanks for your help in advance.
> Kind regards,
> Amir
>   
> write.table(result, "result.csv", quote=F, row.names = T, sep =",")
>> writeWorksheetToFile(output.file, data = result, sheet = "AllSteps",
> + startRow = 1, startCol = 1)
> Error: IllegalArgumentException (Java): File extension "csv" not supported! Only *.xls and *.xlsx are allowed!
>
>
> --- On Wed, 22/8/12, Rui Barradas <ruipbarradas at sapo.pt> wrote:
>
>
> From: Rui Barradas <ruipbarradas at sapo.pt>
> Subject: Re: [R] Appending many different and separate Excel files using R
> To: "Amir Kasaeian" <amir_kasaeian at yahoo.com>
> Cc: "r-help" <r-help at r-project.org>
> Date: Wednesday, 22 August, 2012, 6:14 PM
>
>
> Hello,
>
> I'm glad it helped.
>
> As for books on R, there are so many nowadays that it's difficult to
> recommend one. I'd start by seeing the R home page
> http://www.r-project.org/ and the links on the lower left, under the title
> Documentation > Books
>    or, for free downloads,
> Documentation > Other > contributed documentation
>
> The links also point to the R Wiki, http://rwiki.sciviews.org/doku.php.
> In fact, the large number of documentation, code examples, etc, make of
> Google a very good friend :)
> To make it better, and more complete, some of the web pages are very
> general, others dedicated to certain fields
> Examples of usefull links, one of each type, are, for instance,
>
> http://stackoverflow.com/questions/tagged/r
> http://addictedtor.free.fr/graphiques/
>
> And many others.
> And, of course, the manuals that come with R.
>
> Rui Barradas
>
> Em 22-08-2012 06:20, Amir Kasaeian escreveu:
>> Dear Rui,
>>     
>> Good day!
>>     
>> Thank you very much. The great codes!
>> My other question is that which book or books do you introduce me If I ask you to recommend some R best books, especially in the field of programming?
>> Thanks all your attention.
>>     
>> Kind regards,
>> Amir Kasaeian
>>
>>
>>
>> Amir Kasaeian,
>> PhD Student in Biostatistics,
>> Dept. of Epidemiology and Biostatistics,
>> School of Public Health,
>> Tehran University of Medical Sciences (TUMS).
>> P.B. : 14155-6446
>> Cell Phone: +98-912-2063511
>> E-mail: akasaeian at razi.tums.ac.ir
>>                amir_kasaeian at yahoo.com
>>
>>
>> --- On Sat, 18/8/12, Rui Barradas <ruipbarradas at sapo.pt> wrote:
>>
>>
>> From: Rui Barradas <ruipbarradas at sapo.pt>
>> Subject: Re: [R] Appending many different and separate Excel files using R
>> To: "Amir Kasaeian" <amir_kasaeian at yahoo.com>
>> Cc: "r-help" <r-help at r-project.org>
>> Date: Saturday, 18 August, 2012, 10:33 PM
>>
>>
>> Hello,
>>
>> Try the following.
>>
>>
>> # This needs several other packages
>> # install.packages("XLConnect")
>> require(XLConnect)
>>
>> fpattern <- "Book.*.xls?"  # pattern for filenames
>> output.file <- "Test.xls"
>> lfiles <- list.files(pattern = fpattern)
>>
>> # Read first worksheet from each file
>> data.lst <-lapply(lfiles, readWorksheetFromFile, sheet = 1)
>>
>> # Get column names from data.frames
>> data.names <- lapply(data.lst, names)
>>
>> # Create a result data.frame, we are going
>> # to put everything here
>> result <- data.frame(Book = 1, data.lst[[1]])
>>
>> # Work variable, which column names do we
>> # already have in 'result'
>> current.names <- c("Book", data.names[[1]])
>> #
>> for(i in seq_along(data.lst)[-1]){
>>         new.names <- setdiff(data.names[[i]], current.names)
>>         current.names <- union(current.names, new.names)
>>         for(nm in new.names)
>>             result[[ nm ]] <- NA
>>         tmp <- as.data.frame(matrix(nrow = nrow(data.lst[[i]]), ncol =
>> ncol(result)))
>>         names(tmp) <- current.names
>>         tmp[[ "Book" ]] <- i
>>         tmp[ , data.names[[i]] ] <- data.lst[[i]]
>>         result <- rbind(result, tmp)
>> }
>>
>> writeWorksheetToFile(output.file, data = result, sheet = "AllBooks",
>> startRow = 1, startCol = 1)
>>
>> Hope this helps,
>>
>> Rui Barradas
>> Em 18-08-2012 00:04, Amir Kasaeian escreveu:
>>> Dear Rui,
>>>
>>> Thank you very much for your message.
>>> Suppose we have 3 Excel files: Book1, Book2 and Book3 in a single directory. Please note the headers and their order. I need an output EXCEL file like Amir which includes all the information of the 3 Booki files. I think with the attached file I explain my problem.
>>> As a matter of fact, I need to creat a new file named Amir which has the information of the same and different variable from different excel file with a single spreadsheet below each others and it has the new variable named "Book" indicate which row come from which file.
>>> Is my information enough ?
>>>
>>> Again, thank you very much.
>>>
>>> Best,
>>> Amir
>>>
>>>
>>>
>>> Amir Kasaeian,
>>> PhD Student in Biostatistics,
>>> Dept. of Epidemiology and Biostatistics,
>>> School of Public Health,
>>> Tehran University of Medical Sciences (TUMS).
>>> P.B. : 14155-6446
>>> Cell Phone: +98-912-2063511
>>> E-mail: akasaeian at razi.tums.ac.ir
>>>                  amir_kasaeian at yahoo.com
>>>
>>>
>>> --- On Sat, 18/8/12, Rui Barradas <ruipbarradas at sapo.pt> wrote:
>>>
>>>
>>> From: Rui Barradas <ruipbarradas at sapo.pt>
>>> Subject: Re: [R] Appending many different and separate Excel files using R
>>> To: "Amir Kasaeian" <amir_kasaeian at yahoo.com>
>>> Cc: "r-help" <r-help at r-project.org>
>>> Date: Saturday, 18 August, 2012, 3:37 AM
>>>
>>>
>>>
>>> Hello,
>>>
>>> You need to provide us with a bit more information:
>>> Do all the files have the same structure, i.e., tables with the same columns? No Are they xls, xlsx or csv files? they may be in each of the three types Do their names share something in common such as a preffix or are they alone in the directory, or...?  No, they are with different name without sth in common When you  write "in the final file" does this mean that you want to output two files, one with the data and the other with the filenames No, Just one file including all the variable from different files with new variable indicating which row of data come from which file
>>> It would be nice if (a) the files were csv files, (b) if you answer to some or all of the rest, (c) anything that I might have forgotten and you find usefull.
>>> I think nothing left to say.
>>> Rui Barradas thank you very much Rui
>>>
>>>
>>> Em 17-08-2012 22:19, Amir Kasaeian escreveu:
>>>
>>> Dear all,
>>> Good day!
>>> I have a problem in reading Excel files in R and appending them to each other. Suppose we have several Excel files in a directory with headers and want to use R to append them in a single file with an additional variable in the final file indicating from which files the data come from.
>>> As I have many Excel files and their sizes are very big I should write a loop in R to do the work.
>>> I will be very happy if you guide and help me to write the codes.
>>> Thank you very much in advance.
>>>
>>> Kind regards,
>>> Amir Kasaeian
>>>
>>>
>>>
>>>
>>> Amir Kasaeian,
>>> PhD Student in Biostatistics,
>>> Dept. of Epidemiology and Biostatistics,
>>> School of Public Health,
>>> Tehran University of Medical Sciences (TUMS).
>>> P.B. : 14155-6446
>>> Cell Phone: +98-912-2063511
>>> E-mail: akasaeian at razi.tums.ac.ir
>>>                  amir_kasaeian at yahoo.com
>>>
>>>        [[alternative HTML version deleted]]
>>>
>>>
>>>
>>>       
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>>
>



More information about the R-help mailing list