Calculating days per month between interval of two dates

前端 未结 2 565
无人及你
无人及你 2021-01-18 17:53

I have a set of events that each have a start and end date, but they take place over the scope of a number of months. I would like to create a table that shows the number of

2条回答
  •  野的像风
    2021-01-18 18:25

    This is not necessarily efficient because it creates a sequence of days, but it does the job:

    > library(zoo)
    > table(as.yearmon(seq(event_start_date, event_end_date, "day")))
    
    Oct 2012 Nov 2012 Dec 2012 Jan 2013 Feb 2013 
           9       30       31       31        7
    

    If your time span is so large than this method is slow, you'll have to create a sequence of firsts of the months between your two (truncated) dates, take the diff, and do a little extra work for the end points.

提交回复
热议问题