I have a data set which has order date starting from 01/07/2017 to 30/06/2018. I want to extract the week number .I want to assign week number 01 for the days corresponding to 01/07 to 07/07 and so on. I have used the lubridate package but it takes the 01/01 as the starting point.Is there a way to set the offset value ? My code:
order$start_week_date <- floor_date(as.Date(order$order_date),
unit="week" , week_start = getOption("lubridate.week.start", 1))
order$week_no <- strftime( order$start_week_date ,format="%V")
How do you like this approach?
customweek <- function(dateweek, dateorigin){
return(as.integer(round((ymd(dateweek)-ymd(dateorigin) + 1)/7,0)))
}
dataweek will be your date and dateorigin your starting point (both in YYYY/MM/DD format). Therefore:
customweek("20170107", "20170101")
Produces 1 as result.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With