[R] Writing a single output file

jim holtman jholtman at gmail.com
Thu Dec 23 14:39:19 CET 2010


This should get you close:

> # get file names
> setwd('/temp')
> fileNames <- list.files(pattern = "file.*.csv")
> fileNames
[1] "file1.csv" "file2.csv" "file3.csv" "file4.csv"
> input <- do.call(rbind, lapply(fileNames, function(.name){
+     .data <- read.table(.name, header = TRUE, as.is = TRUE)
+     # add file name to the data
+     .data$file <- .name
+     .data
+ }))
> input
        date yield_rate      file
1 12/23/2010       5.25 file1.csv
2 12/22/2010       5.19 file1.csv
3 12/23/2010       5.25 file2.csv
4 12/22/2010       5.19 file2.csv
5 12/23/2010       5.25 file3.csv
6 12/22/2010       5.19 file3.csv
7 12/23/2010       5.25 file4.csv
8 12/22/2010       5.19 file4.csv
> require(reshape)
> in.melt <- melt(input, measure = 'yield_rate')
> cast(in.melt, date ~ file)
        date file1.csv file2.csv file3.csv file4.csv
1 12/22/2010      5.19      5.19      5.19      5.19
2 12/23/2010      5.25      5.25      5.25      5.25
>


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
>
>
> .................................
>
>
> .................................
>
>
>
> Thus all the output files have same column names viz. Date and yield_rate. Also, I do need these files individually too.
>
> My further requirement is to have a single dataframe as given below.
>
> Date             yield_rate1               yield_rate2                yield_rate3
> 12/23/2010       5.25                          4.16                          6.15
> 12/22/2010       5.19                          4.59                          6.41
> ...............................................................................................
> ...............................................................................................
>
> where yield_rate1 = output1$yield_rate and so on.
>
> One way is to simply create a dataframe as
>
> df = data.frame(Date = read.csv('output1.csv')$Date, yield_rate1 =  read.csv('output1.csv')$yield_rate,   yield_rate2 = read.csv('output2.csv')$yield_rate,
> yield_rate3 = read.csv('output3.csv')$yield_rate)
>
> However, the problem arises when I am not aware how many output files are there as n can be 5 or even 100.
>
> So is it possible to write some loop or some function which will enable me to read 'n' files individually and then keeping "Date" common, only pickup the yield_curve data from each output file.
>
> Thanking in advance for any guidance.
>
> Regards
>
> Amy
>
>
>
>
>
>        [[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.
>
>



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?



More information about the R-help mailing list