[R] How to use Sys.time() while writing a csv file name
Jeff Newmiller
jdnewmil at dcn.davis.CA.us
Wed Jul 4 07:38:00 CEST 2012
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.
More information about the R-help
mailing list