Reputation: 421
I used week() and strftime() to convert day to week in R, but the functions set start day from Monday not Sunday, converting calendar year day to fiscal year week becomes off. My fiscal year starts from October 1st, and 2017-10-01 has to be week 1. However, strftime() counts week from monday, week of'2017-10-01', which is sunday, subtracts '2017-9-30', which is saturday, becomes 0 instead of 1.
> as.numeric(strftime("2017-10-1", format = "%V"), format = "%V")-as.numeric(strftime("2017-9-30", format = "%V"))
[1] 0
How can I convert calendar day to fiscal year week that counts a week from Sunday?
Upvotes: 1
Views: 678
Reputation: 388982
Use "%U"
which counts week from Sunday. From ?strptime
%U - Week of the year as decimal number (00–53) using Sunday as the first day 1 of the week (and typically with the first Sunday of the year as day 1 of week 1). The US convention.
as.numeric(strftime("2017-10-1", format = "%U")) -
as.numeric(strftime("2017-9-30", format = "%U"))
#[1] 1
You can also use week
from lubridate
library(lubridate)
week(ymd('2017-10-1')) - week(ymd('2017-9-30'))
#[1] 1
Upvotes: 1