round_any on Date objects

50 views
Skip to first unread message

Ivan Alves

unread,
Dec 5, 2008, 9:05:22 AM12/5/08
to ggplot2
Hi,

I would like to group observations for a boxplot with round_any(), but
it turns out that no method is defined for Date objects (I would like
to group daily observations in weekly of monthly bins). Any
suggestion on which functions/packages I could use? Many thanks in
advance.

Kind regards,
Ivan

hadley wickham

unread,
Dec 5, 2008, 9:07:13 AM12/5/08
to Ivan Alves, ggplot2
Hi Ivan,

Have a look at ?round.Date

Hadley
--
http://had.co.nz/

Ivan Alves

unread,
Dec 5, 2008, 10:01:35 AM12/5/08
to ggplot2
Thanks Hadley. Unfortunately "methods for class "Date" are of little
use except to remove fractional days", and therefore does not work for
grouping daily data in weeks or months. Should I tinker around with
as.numeric() and round_any()?

hadley wickham

unread,
Dec 5, 2008, 10:14:00 AM12/5/08
to Ivan Alves, ggplot2
Ooooooh, I had forgotten about that. You might want to have a look at
the code for floor_date and ceiling_date in ggplot2 - I wrote these
methods to work around some of my frustrations with date time
functionality in R. I didn't implement a round function, but I think
you should be able to
--
http://had.co.nz/

Wayne Folta

unread,
Dec 5, 2008, 10:13:40 AM12/5/08
to Ivan Alves, ggplot2

>Thanks Hadley. Unfortunately "methods for class "Date" are of little
>use except to remove fractional days", and therefore does not work for
>grouping daily data in weeks or months. Should I tinker around with
>as.numeric() and round_any()?

If I understand your original question, cut.POSIXt sounds like it could bin your time data for you.

Wayne

hadley wickham

unread,
Dec 5, 2008, 10:15:56 AM12/5/08
to Ivan Alves, ggplot2
Sorry - hit sent too soon.

You should be able to implement round_date by modifying ceiling_date.
You can use the functions like:

floor_date(mydate, "5 day")
floor_date(mydate, "2 week")
floor_date(mydate, "month")

etc.

But I should make round_any generic - I'll add that to my to do.

Hadley
--
http://had.co.nz/

hadley wickham

unread,
Dec 5, 2008, 11:29:03 AM12/5/08
to Wayne Folta, Ivan Alves, ggplot2

Good point - that's what floor_date and ceiling_date are based on.
Unfortunately there are a few annoying bugs in cut.POSIXt that you
need to work around. I've reported them on R-help, but there doesn't
seem to be any desire to fix them.

Hadley


--
http://had.co.nz/

Ivan Alves

unread,
Dec 5, 2008, 11:34:12 AM12/5/08
to ggplot2
In fact (many thanks Wayne!) cut() does the job.

Many thanks to Hadley and Wayne.
Reply all
Reply to author
Forward
0 new messages