[R] Saving Splitted Series to Excel via XLConnect

David Winsemius dwinsemius at comcast.net
Thu Aug 9 01:07:21 CEST 2012


On Aug 8, 2012, at 1:10 PM, Henrique Andrade wrote:

> Dear R Discussion List,
>
> I would like to save my data as a xlsx file. But at first
> I need to split it and then save each series into a Excel
> column.

That doesn't make much sense. You have three columns. What are you  
calling a "series" (which is not a term that has any particular  
meaning in R.)

> Please take a look at the following code:
>
> dados <- data.frame(matrix(c("2012-01-01","2012-02-01",
>  "2012-03-01","2012-04-01","2012-05-01","2012-06-01",
>  "2012-01-01","2012-02-01","2012-03-01","2012-04-01",
>  "2012-05-01","2012-06-01","2012-01-01","2012-02-01",
>  "2012-03-01","2012-04-01","2012-05-01","2012-06-01",
>  0.56,0.45,0.21,0.64,0.36,0.08,152136,153081,155872,
>  158356,162157,166226,33.47,34.48,35.24,38.42,35.33,
>  34.43,433,433,433,433,433,433,2005,2005,2005,2005,
>  2005,2005,3939,3939,3939,3939,3939,3939),nrow=18,
>  ncol=3,byrow=FALSE,dimnames=list(c(1,2,3,4,5,6,7,8,9,
>  10,11,12,13,14,15,16,17,18),c("date","value","code"))))
>
> dados2 <- split(dados, dados$code)
> dados2
>
> library(XLConnect)
>
> wb <- loadWorkbook("Teste.xlsx", create = TRUE)
> createSheet(wb, name = "Teste1")
> writeWorksheet(wb, dados2, sheet = "Teste1")
> saveWorkbook(wb)

Perhaps:

str(dados2)  # it is a list of three dataframes

writeWorksheet(wb, do.call(rbind, dados2), sheet = "Teste1")
saveWorkbook(wb)

This basically undoes the split() operation that created dados2.

Please note that those dates are actually factor variables in R.
>
> With this code I only get the "433" series. How could I
> fix my code? How could I include the other series?
>
> Many thanks in advance,
> Henrique Andrade
>
-- 

David Winsemius, MD
Alameda, CA, USA



More information about the R-help mailing list