Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: cut function with breaks for every two months

Tags:

r

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.
like image 550
user3684014 Avatar asked Oct 20 '25 14:10

user3684014


1 Answers

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').

like image 146
Gregor Thomas Avatar answered Oct 23 '25 05:10

Gregor Thomas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!