Reputation: 35
I have a couples of weeknumbers of interest. Lets take '202124' (this week) as an example. How can I subtract x weeks from this week number?
Lets say I want to know the week number of 2 weeks prior, ideally I would like to do 202124 - 2
which would give me 202122
. This is fine for most of the year however 202101 - 2
will give 202099
which is obviously not a valid week number. This would happen on a large scale so a more elegant solution is required. How could I go about this?
Upvotes: 0
Views: 640
Reputation: 388982
convert the year week values to dates subtract in days and format the output.
x <- c('202124', '202101')
format(as.Date(paste0(x, 1), '%Y%W%u') - 14, '%Y%V')
#[1] "202122" "202052"
To convert year week value to date we also need day of the week, I have used it as 1st day of the week.
Upvotes: 1