Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto populate week dates

Tags:

date

r

I'm trying to find a way to auto populate the start/end week dates for any given year. E.g. 4/12/2015 - 4/18/2015, 4/19/2015 - 4/25/2015, 4/26/2015 - 5/2/2015. I guess I could calculate those explicitly, but it's not very elegant. Thanks in advance!

like image 272
Alexey Ferapontov Avatar asked Apr 14 '15 13:04

Alexey Ferapontov


People also ask

Can you use autofill to get a list of weekdays?

AutoFill Weekday NamesSelect the cell, and point to the Fill Handle, at the bottom right corner of the selected cell. Press the left mouse button, and drag (up, down, left or right) -- the weekday names will appear in a pop-up tip, near the pointer.


1 Answers

Find first day and last days of the first week of year: read the %W, %w, %V specifications under ?strptime for more detail. Be aware that strptime conventions are very finicky (e.g. you need exactly the right number of digits), possibly operating-system-dependent, and possibly locale-dependent ...

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 up date sequences 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 e.g. paste0(year,"-01-0") if you want to do this more generally (without hard-coding the year).

like image 125
Ben Bolker Avatar answered Oct 06 '22 00:10

Ben Bolker