Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning custom week number in R

Tags:

r

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") 
like image 928
sanjeev kumar ojha Avatar asked Jan 28 '19 16:01

sanjeev kumar ojha


1 Answers

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.

like image 104
LocoGris Avatar answered Sep 28 '22 01:09

LocoGris