我有一组事件,每个事件都有一个开始和结束日期,但它们发生在几个月的范围内。我想创建一个表,显示此活动每月的天数。
我有以下例子。
event_start_date< - as.Date(23/10/2012,%d /%m /%Y ) event_end_date< - as.Date(07/02/2013,%d /%m /%Y)我希望得到一个表如下:
Oct-12 8 Nov-12 30 Dec-12 31 Jan-13 31 Feb-13 7Jochem
解决方案这不一定是有效的,因为它创建一个顺序的日子,但它的工作:
>图书馆(动物园)>表(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如果你的时间跨度比这个方法太慢,你必须创建一个序列的第一个月在你的两个(截断)日期之间,采取 diff ,并为终点做一点额外的工作。
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 days in each month for this event.
I have the following example.
event_start_date <- as.Date("23/10/2012", "%d/%m/%Y") event_end_date <- as.Date("07/02/2013", "%d/%m/%Y")I would expect to get a table out as the following:
Oct-12 8 Nov-12 30 Dec-12 31 Jan-13 31 Feb-13 7Does anybody know about a smart and elegant way of doing this or is creating a system of loops the only viable method?
Jochem
解决方案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 7If 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.
更多推荐
计算两个日期间隔之间的每月天数
发布评论