[R] Writing a single output file

Gabor Grothendieck ggrothendieck at gmail.com
Thu Dec 23 17:48:48 CET 2010


On Thu, Dec 23, 2010 at 8:07 AM, Amy Milano <milano_amy at yahoo.com> wrote:
> Dear R helpers!
>
> Let me first wish all of you "Merry Christmas and Very Happy New year 2011"
>
> "Christmas day is a day of Joy and Charity,
> May God make you rich in both" - Phillips Brooks
>
> ## ----------------------------------------------------------------------------------------------------------------------------
>
> I have a process which generates number of outputs. The R code for the same is as given below.
>
> for(i in 1:n)
> {
> write.csv(output[i], file = paste("output", i, ".csv", sep = ""), row.names = FALSE)
> }
>
> Depending on value of 'n', I get different output files.
>
> Suppose n = 3, that means I am having three output csv files viz. 'output1.csv', 'output2.csv' and 'output3.csv'
>
> output1.csv
> date               yield_rate
> 12/23/2010        5.25
> 12/22/2010        5.19
> .................................
> .................................
>
>
> output2.csv
>
> date               yield_rate
>
> 12/23/2010        4.16
>
> 12/22/2010        4.59
>
> .................................
>
> .................................
>
> output3.csv
>
>
> date               yield_rate
>
>
> 12/23/2010        6.15
>
>
> 12/22/2010        6.41
>


In the development version of zoo you can do all this in basically one
read.zoo command producing the required zoo series:

# chron's default date format is the same as in the output*.csv files
library(chron)

# pull in development version of read.zoo
library(zoo)
source("http://r-forge.r-project.org/scm/viewvc.php/*checkout*/pkg/zoo/R/read.zoo.R?revision=813&root=zoo")

# this does it
z <- read.zoo(Sys.glob("output*.csv"), header = TRUE, FUN = as.chron)

as.data.frame(z) or data.frame(Time = time(z), coredata(z)) can be
used to convert z to a data frame with times as row names or a data
frame with times in column respectively (although you may wish to just
leave it as a zoo object so you can take advantage of zoo's other
facilities too).


-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list