简体   繁体   中英

How can I change a directory based on the current date in R?

I need to run a report each day. I want to save the raw data and output in a particular folder followed by a sub folder for year, month, date. Eg today's would be

C:\\My Documents\\My Project\\2016\\February\\27.

How would I set this up as my working directory?

Since your goal is to have included backslashes in the string, which are the escape character for R, you will need to double them in the format argument and in the arguments to paste .

 format( Sys.Date(), "%Y\\%B\\%d")
#[1] "2016\\February\\27"

paste0("C:\\My Documents\\My Project\\", format( Sys.Date(), "%Y\\%B\\%d") )
[1] "C:\\My Documents\\My Project\\2016\\February\\27"

You can also switch to (single) forward-slashes which may be less confusing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM