Auto-fill weekly dates

I'm trying to find a way to automatically populate the start and end dates of a week for any year. For instance. 4/12/2015 - 4/18/2015, 4/19/2015 - 4/25/2015, 4/26/2015 - 5/2/2015. I think I could have calculated them explicitly, but it's not very elegant. Thanks in advance!

+6
source share
1 answer

Find the first day and last days of the first week of the year: for more information, see %W , %W , %V in the ?strptime section. Keep in mind that the strptime conventions strptime very subtle (for example, you need exactly the right number of digits), possibly depending on the operating system and, possibly, depending on the language ...

 firstday <- as.Date(strptime("2010-01-0",format="%Y-%W-%w")) ## Sunday lastday <- as.Date(strptime("2010-01-6",format="%Y-%W-%w")) ## Saturday 

Now set the dates starting from those days:

 seq.Date(firstday,as.Date("2010-12-31"),by="1 week") seq.Date(lastday,as.Date("2010-12-31"),by="1 week") 

You can use for example. paste0(year,"-01-0") , if you want to do it more generally (without hard coding in a year).

+6
source

Source: https://habr.com/ru/post/985120/


All Articles