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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Vincy Pyne
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]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to use Sys.time() while writing a csv file name

Pascal Oettli-2
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]]
>
>
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to use Sys.time() while writing a csv file name

Jeff Newmiller
In reply to this post by Vincy Pyne
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:<[hidden email]>        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 <[hidden email]> 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]]
>
>______________________________________________
>[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to use Sys.time() while writing a csv file name

PtitBleu
In reply to this post by Vincy Pyne
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.
Reply | Threaded
Open this post in threaded view
|

Re: How to use Sys.time() while writing a csv file name

Vincy Pyne
In reply to this post by Jeff Newmiller

Dear Mr Newmiller and Mr Oettli,

Thanks a lot for your valuable guidance. Task is done. Thanks again.

Regards

Vincy


--- On Wed, 7/4/12, Jeff Newmiller <[hidden email]> wrote:

From: Jeff Newmiller <[hidden email]>
Subject: Re: [R] How to use Sys.time() while writing a csv file name
To: "Vincy Pyne" <[hidden email]>, [hidden email]
Received: Wednesday, July 4, 2012, 5:38 AM

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:<[hidden email]>        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.



>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]]
>
>______________________________________________
>[hidden email] 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.

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.