awoj
awoj

Reputation: 199

R what is the best way to get previous week number from date if the actual one is 1

I'm looking for the best way to find a previous week number if the actual one is 1, like we have now. I'm always using lubridate::isoweek(date)-1 (I need week starting from Sun) to get previous week number but now this doesn't work since I get 0 and I need 52 (or 53 depending on prev. years), how do you solve it? I can do a lag or other solutions but maybe it's more simpler approach?

Upvotes: 2

Views: 55

Answers (1)

Maël
Maël

Reputation: 51914

You can remove seven days from the date:

library(lubridate)
date = as.Date("2023-01-03")
isoweek(date - days(7))
#[1] 52

Or 1 week:

isoweek(date - weeks(1))
#[1] 52

Upvotes: 2

Related Questions