简体   繁体   中英

R: Get Week Number of a Quarter

I want to get the week number of a quarter in R. Week number begins on the first of the month and the next week starts on Sunday.

Date      WeekNum
1/1/2019  1
1/7/2019  2
2/7/2019  6
4/22/2019 4
5/1/2019  5
10/1/2019 1

In the past I've done it in excel, thanks to Week number of a quarter but now I want to try this in R.

The zoo package would probably be most helpful for you.

library(zoo)
(as.integer(test$Date - as.Date(as.yearqtr(test$Date))) + match(weekdays(test$Date), weekdays(as.Date(3:9))) - 1L) %/% 7L + 1L

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM