[R] How to use Sys.time() while writing a csv file name

Pascal Oettli kridox at ymail.com
Wed Jul 4 07:36:11 CEST 2012


Hello,

Try something like that:

 > lgd <- format(Sys.time(), "%Y_%m_%d_%H_%M_%S")

Regards,
Pascal


Le 04/07/2012 14:21, Vincy Pyne a écrit :
> Dear R helpers,
>
> I am using Beta distribution to generate the random no.s (recovery rates in my example). However, each time I need to save these random no.s in a csv format. To distinguish different csv files, one way I thought was use of Sys.time in the file name. My code is as follows -
>
> # My code
>
> rr = rbeta(25, 6.14, 8.12)
>
> lgd = 1 - mean(rr)
>
> write.csv(data.frame(recovery_rates = rr), file = paste("recovery_rates_at_", Sys.time(), ".csv", sep = ""), row.names = FALSE)
>
>
> However, I get following error -
>
> Error in file(file, ifelse(append, "a", "w")) :   cannot open the connection
> In addition: Warning message: In file(file, ifelse(append, "a", "w")) :
> cannot open file 'recovery_rates_at_2012-07-04 1:14:05.csv': Invalid argument
>
>
> If instead of Sys.time, I use some other variable e.g. lgd as
>
> write.csv(data.frame(recovery_rates = rr), paste('rates_',lgd,'.csv', sep = ""), row.names = FALSE)
>
> I am able to store these simulated recovery rates in different files. But I need to use Sys.time in my csv file name. (or is there any other way of writing these csv files so that files don't get over-written).
>
> Kindly guide.
>
> Regards and thanking in advance
>
> Vincy
>
>
> 	[[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