Guest User

Untitled

a guest
Dec 12th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. week("2000-01-01")
  2. week("2000-01-06")
  3. week("2000-01-07")
  4. week("2000-01-13")
  5. week("2000-01-14")
  6.  
  7. require(lubridate)
  8. dates <- c("2000-01-01","2000-01-06","2000-01-07","2000-01-13","2000-01-14")
  9. sapply(dates, isoweek)
  10. 2000-01-01 2000-01-06 2000-01-07 2000-01-13 2000-01-14
  11. 52 1 1 2 2
  12.  
  13. require(lubridate)
  14. my_week <- function(x){
  15. # fst monday of the same year
  16. first_sun <- as.POSIXct(paste0(year(x),"-01-Mon"), format = "%Y-%U-%a")
  17. (yday(x) + (7 - yday(first_sun) + 1)) %/% 7
  18. }
  19.  
  20. dates <- seq(as.Date("2000-01-01"), as.Date("2000-01-15"), by=1)
  21. a <- sapply(dates, my_week)
  22. names(a) <- dates
  23.  
  24. > a
  25. 2000-01-01 2000-01-02 2000-01-03 2000-01-04 2000-01-05
  26. 0 1 1 1 1
  27. 2000-01-06 2000-01-07 2000-01-08 2000-01-09 2000-01-10
  28. 1 1 1 2 2
  29. 2000-01-11 2000-01-12 2000-01-13 2000-01-14 2000-01-15
  30. 2 2 2 2 2
Add Comment
Please, Sign In to add comment