r - Getting the week from the date -
i have series of dates , trying week of year. seems innacurate.
order_date = c("2017-06-10","2017-06-11","2017-06-12","2017-06-13", "2017-06-14","2017-06-15","2017-06-16") strftime(as.posixlt(order_date) ,format="%w") week(order_date)
06/10 saturday , there's no way 06/10 , 06/11 of same week.
any ideas on how this?
working date-time not straightforward. illustration of question , comments members. added few days make point more clear.
library(lubridate) order_date = c("2017-06-10","2017-06-11","2017-06-12","2017-06-13", "2017-06-14","2017-06-15","2017-06-16", "2017-06-17", "2017-06-18", "2017-06-19") strftime(as.posixlt(order_date) ,format="%w") # week starts on monday # [1] "23" "23" "24" "24" "24" "24" "24" "24" "24" "25" strftime(as.posixlt(order_date) ,format="%u") # week starts on sunday # [1] "23" "24" "24" "24" "24" "24" "24" "24" "25" "25" week(order_date) # week starts on sunday # [1] 23 24 24 24 24 24 24 24 25 25
in related question: here, used @uweblock 's isoweek
package circumvent problems year-ends after extensive testing. here code:
library(isoweek) library(stringr) str_replace(isoweek(order_date), "201.-w", "") # week starts on monday (and follows iso 8601) # [1] "23" "23" "24" "24" "24" "24" "24" "24" "24" "25"
you can more information ?isoweek
.
please let me know whether want.
Comments
Post a Comment