Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. DaysToMatch <- as.Date(c("1991-03-30", "1993-12-18"))
  2.  
  3. countDays <- function(initialdate, enddate, DaysToMatch) {
  4. measurementwindow <- seq(as.Date(initialdate), as.Date(enddate), by="days")
  5. commonDays <- length(intersect(measurementwindow, DaysToMatch))
  6. #Or sum(DaysToMatch %in% measurementwindow)
  7. return(commonDays)
  8. }
  9.  
  10. df$commonDays <- mapply(countDays, df$initialdate, df$enddate,
  11. MoreArgs = list(DaysToMatch = DaysToMatch))
  12.  
  13. df
  14. # individual initialdate enddate RGR commonDays
  15. #1 1 1990-03-01 1991-08-05 0.25 1
  16. #2 2 1991-05-07 1993-02-17 0.30 0
  17. #3 3 1992-05-02 1995-06-22 0.09 1
  18. #4 4 1994-03-02 1995-09-12 0.48 0
  19. #5 5 1995-03-08 1997-08-18 0.34 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement