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]]
Pascal Oettli
2012-Jul-04 05:36 UTC
[R] How to use Sys.time() while writing a csv file name
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. >
Jeff Newmiller
2012-Jul-04 05:38 UTC
[R] How to use Sys.time() while writing a csv file name
You forgot to follow the posting guide and tell us what operating system you are using (sessionInfo), but I am going to guess that you are on Windows where the colon (":") is an illegal symbol in filenames. Try formatting the time explicitly in the conversion to character using the format string definitions found in ?strptime in a format that doesn't include colons. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. Vincy Pyne <vincy_pyne at yahoo.ca> wrote:>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.
Hello, It seems that there is a problem with ":". If you only need the date, you can use as.Date(Sys.time()) instead of the complete form. If you need the time, you can try the following commands which change the ":" into "-" : newsystime<-<-format(Sys.time(),"%Y-%m-%d-%H-%M-%S") write.csv(data.frame(recovery_rates = rr), file paste("recovery_rates_at_", newsystime, ".csv", sep = ""), row.names FALSE) Have a good day, Ptit Bleu. -- View this message in context: http://r.789695.n4.nabble.com/How-to-use-Sys-time-while-writing-a-csv-file-name-tp4635358p4635363.html Sent from the R help mailing list archive at Nabble.com.