Guest User

Untitled

a guest
Jan 22nd, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. ## Make data easily reproducible
  2. df <- data.frame(day=c(24, 21, 20, 10, 20, 20, 10, 15),
  3. month = c("Jun", "Mar", "Jan", "Dec", "Jun", "Jan", "Dec", "Dec"))
  4.  
  5. ## Convert each month-day combo to its corresponding "julian date"
  6. datestring <- paste("1963", match(df[[2]], month.abb), df[[1]], sep = "-")
  7. date <- strptime(datestring, format = "%Y-%m-%d")
  8. julian <- as.integer(strftime(date, format = "%j"))
  9.  
  10. ## Transitions between years occur wherever julian date increases between
  11. ## two observations
  12. df$year <- 1963 - cumsum(diff(c(julian[2], julian))>0)
  13.  
  14. df$year <- 1963 + cumsum(c(0L, diff(100L*as.integer(
  15. factor(df$month, levels = month.abb)) + df$day) < 0))
  16. df
  17.  
  18. day month year
  19. 1 24 Jun 1963
  20. 2 21 Mar 1964
  21. 3 20 Jan 1965
  22. 4 10 Dec 1965
  23. 5 20 Jun 1966
  24. 6 20 Jan 1967
  25. 7 10 Dec 1967
  26. 8 15 Dec 1967
Add Comment
Please, Sign In to add comment