[R] Truncating dates (and other date-time manipulations)

Marc Schwartz marc_schwartz at comcast.net
Fri Sep 12 00:31:42 CEST 2008


on 09/11/2008 04:42 PM hadley wickham wrote:
> On Thu, Sep 11, 2008 at 11:00 AM, Gabor Grothendieck
> <ggrothendieck at gmail.com> wrote:
>> See ?cut.Date
>>
>> In the zoo package see:
>> ?as.yearmon
>> ?as.yearqtr
>> ?aggregate.zoo
>>
>> and the many examples in:
>> ?plot.zoo
>> ?xyplot.zoo
>> as well as the three zoo vignettes.
>>
>> Also in the xts package look at
>> ?to.period
>>
>> For regularly spaced series the tis package supports a wide
>> variety of time bases and can convert among them.
>>
>> There are as.zoo.tis and as.tis.zoo routines in zoo and tis; also,
>> xts is a subclass of zoo so all these packages can work together.
> 
> Ok, lets take a concrete example:
> 
> dates <- structure(c(8516, 8544, 8568, 8596, 8609, 8666, 8701, 8750, 8754,
> 8798, 8811, 8817, 8860, 8873, 8918, 8931, 8966, 9020, 9034, 9056
> ), class = "Date")
> 
>> range(dates)
> [1] "1993-04-26" "1994-10-18"
> 
> I want to be able to say: give me a monthly time series that spans
> this range - i.e. it should start on 1993-04-01 and continue to
> 1994-11-01.  I should equally easily be able to say give me a yearly
> time series, or every 2 months or every 3 months, or 2 weeks etc.
> 
> I don't think that cut.Date helps because I want to make a new series,
> not divide up an existing one, similarly with to.period.  as.yearmon,
> as.yearqtr etc, might be helpful but I'd need to stitch them together
> myself and they don't return dates so I'd have to convert back for
> plotting.   plot.zoo doesn't help because all the examples are regular
> time series.

Hadley,

What's wrong with:

dates <- structure(c(8516, 8544, 8568, 8596, 8609, 8666, 8701, 8750,
                     8754, 8798, 8811, 8817, 8860, 8873, 8918, 8931,
                     8966, 9020, 9034, 9056), class = "Date")


> as.Date(cut.Date(dates, "year"))
 [1] "1993-01-01" "1993-01-01" "1993-01-01" "1993-01-01" "1993-01-01"
 [6] "1993-01-01" "1993-01-01" "1993-01-01" "1993-01-01" "1994-01-01"
[11] "1994-01-01" "1994-01-01" "1994-01-01" "1994-01-01" "1994-01-01"
[16] "1994-01-01" "1994-01-01" "1994-01-01" "1994-01-01" "1994-01-01"


> as.Date(cut.Date(dates, "month"))
 [1] "1993-04-01" "1993-05-01" "1993-06-01" "1993-07-01" "1993-07-01"
 [6] "1993-09-01" "1993-10-01" "1993-12-01" "1993-12-01" "1994-02-01"
[11] "1994-02-01" "1994-02-01" "1994-04-01" "1994-04-01" "1994-06-01"
[16] "1994-06-01" "1994-07-01" "1994-09-01" "1994-09-01" "1994-10-01"


> as.Date(cut.Date(dates, "2 months"))
 [1] "1993-04-01" "1993-04-01" "1993-06-01" "1993-06-01" "1993-06-01"
 [6] "1993-08-01" "1993-10-01" "1993-12-01" "1993-12-01" "1994-02-01"
[11] "1994-02-01" "1994-02-01" "1994-04-01" "1994-04-01" "1994-06-01"
[16] "1994-06-01" "1994-06-01" "1994-08-01" "1994-08-01" "1994-10-01"


> as.Date(cut.Date(dates, "3 months"))
 [1] "1993-04-01" "1993-04-01" "1993-04-01" "1993-07-01" "1993-07-01"
 [6] "1993-07-01" "1993-10-01" "1993-10-01" "1993-10-01" "1994-01-01"
[11] "1994-01-01" "1994-01-01" "1994-04-01" "1994-04-01" "1994-04-01"
[16] "1994-04-01" "1994-07-01" "1994-07-01" "1994-07-01" "1994-10-01"


> as.Date(cut.Date(dates, "quarter"))
 [1] "1993-04-01" "1993-04-01" "1993-04-01" "1993-07-01" "1993-07-01"
 [6] "1993-07-01" "1993-10-01" "1993-10-01" "1993-10-01" "1994-01-01"
[11] "1994-01-01" "1994-01-01" "1994-04-01" "1994-04-01" "1994-04-01"
[16] "1994-04-01" "1994-07-01" "1994-07-01" "1994-07-01" "1994-10-01"


> as.Date(cut.Date(dates, "2 weeks"))
 [1] "1993-04-26" "1993-05-24" "1993-06-07" "1993-07-05" "1993-07-19"
 [6] "1993-09-13" "1993-10-25" "1993-12-06" "1993-12-20" "1994-01-31"
[11] "1994-02-14" "1994-02-14" "1994-03-28" "1994-04-11" "1994-05-23"
[16] "1994-06-06" "1994-07-18" "1994-09-12" "1994-09-26" "1994-10-10"


You can of course, format the dates as needed for aesthetic purposes.

Isn't that what you seem to be looking for?

BTW, seq.Date() supports the same time intervals designations.

HTH,

Marc Schwartz



More information about the R-help mailing list