Skip to content

cut.Date functionality for chron date/time objects

5 messages · Gabor Grothendieck, Spencer Graves, Sébastien Bihorel

#
Hello,

I've encountered the need to cut some chron objects of the form:

R> mychron <- chron(sort(runif(10, 0, 10)))
R> mychron
 [1] (01/01/70 16:36:20) (01/02/70 00:08:46) (01/03/70 16:54:49)
 [4] (01/04/70 06:45:00) (01/07/70 06:21:24) (01/07/70 18:28:44)
 [7] (01/08/70 00:47:05) (01/08/70 05:11:44) (01/10/70 01:07:53)
[10] (01/10/70 17:46:53)

into arbitrary (e.g. a given number of minutes, seconds, etc.) units.
Basically, I'm searching for the functionality of cut.Date, which has a
nice 'breaks' argument:


,-----[ *help[R](cut.Date)* (lines: 23 - 30) ]
|   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"' or '"year"', optionally
|           preceded by an integer and a space, or followed by '"s"'. 
|           For '"Date"' objects only '"day"', '"week"', '"month"' and
|           '"year"' are allowed. 
`-----

'cut.dates' (from chron itself) allows for cut'ing into months, years,
etc., but not any fractions or multiples of these.

Converting the chron objects to POSIXct (via 'as.POSIXct') and then using
cut.Date works, but is inconvenient, as it introduces time zone
information. Is there a better way to deal with this?

Thanks in advance,
#
Assuming, as in your post:

set.seed(123)
mychron <- chron(sort(runif(10, 0, 10)))
breaks <- quantile(mychron)

# is one of these adequate?

cut(mychron, breaks)

cut(unclass(mychron), unclass(breaks), lab = FALSE)
On 8/2/05, Sebastian Luque <spluque at gmail.com> wrote:
#
Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
Thank you Gabor, that showed me I really needed to be more creative with
the 'breaks' argument. So what I needed was (with mychron as above):

breaks <- seq(min(mychron, na.rm = TRUE),
              ceiling(max(mychron, na.rm = TRUE)), by = 1/2)

cut(unclass(mychron), unclass(breaks),
    include.lowest = TRUE, labels = FALSE)


in order to cut the chron object into 1/2 day units.


Thanks so much,
#
How about the following:

 > set.seed(123)
 > mychron <- chron(sort(runif(10, 0, 10)))
 > (breaks <- chron(pretty(quantile(mychron))))
[1] 01/01/70 01/03/70 01/05/70 01/07/70 01/09/70 01/11/70
 >
spencer graves
Sebastian Luque wrote:

            

  
    
#
Spencer Graves <spencer.graves at pdf.com> wrote:
I was looking for a way to cut it into specified units of time, rather
than quantiles. Anyway, both your suggestions show one can go very far
with the breaks arg.

Thank you!