user3684014
user3684014

Reputation: 1175

R: cut function with breaks for every two months

I've learnt from somewhere that for Date vector we can use cut to split into bins:

cut(dates, breaks='quarter')

Now I want to change it so that it breaks by every two months, how can I do that?

I try to look it up by ?cut, but it won't even show that I can use 'quarter' or 'month' to break. Where can I find a more detailed document about this usage?

breaks: 
either a numeric vector of two or more unique cut points or a single number (greater than or equal to 2) giving the number of intervals into which x is to be cut.

Upvotes: 4

Views: 5107

Answers (2)

user9115261
user9115261

Reputation: 1

You can use the following:

cut(dates, breaks='2 month')

Upvotes: 0

Gregor Thomas
Gregor Thomas

Reputation: 145775

This is a bit of an unfortunate side-effect of S3 classes: you need to make sure you're looking at the appropriate help. Don't look at ?cut, look at ?cut.Date or ?cut.POSIXt (as BondedDust suggests).

From that help:

breaks a vector of cut points or number giving the number of intervals which x is to be cut into or an interval specification, one of "sec", "min", "hour", "day", "DSTday", "week", "month", "quarter" or "year", optionally preceded by an integer and a space, or followed by "s". For "Date" objects only "day", "week", "month", "quarter" and "year" are allowed.

As @akrun says, for your case you can use cut(dates, breaks='2 months').

Upvotes: 11

Related Questions