简体   繁体   English

如何根据R中的当前日期更改目录?

[英]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. 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 . 由于您的目标是在字符串中包含反斜杠(R的转义字符),因此您需要在format参数和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. 您还可以切换到(单个)正斜杠,这样可能会减少混乱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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