Split date into YYYY-MM-DD-HH-MM-SS and aggregate date (R)
<p>How can one split the following <code>datetime</code> into <code>year-month-day-hour-minute-second</code>? The date was created using: </p>

<pre><code>datetime = seq.POSIXt(as.POSIXct("2015-04-01 0:00:00", tz = 'GMT'), 
                                           as.POSIXct("2015-11-30 23:59:59", tz = 'GMT'), 
                                           by="hour",tz="GMT"))
</code></pre>

<p>The ultimate goal is to aggregate <code>x</code> which is at <code>hourly</code> resolution into <code>6-hourly</code> resolution. Probably it is possible to <code>aggregate datetime</code> without needing to split it? </p>

<pre><code>             datetime     x 
1  2015-04-01 00:00:00    0.0     
2  2015-04-01 01:00:00    0.0     
3  2015-04-01 02:00:00    0.0     
4  2015-04-01 03:00:00    0.0     
5  2015-04-01 04:00:00    0.0     
6  2015-04-01 05:00:00    0.0     
7  2015-04-01 06:00:00    0.0     
8  2015-04-01 07:00:00    0.0     
9  2015-04-01 08:00:00    0.0     
10 2015-04-01 09:00:00    0.0     
11 2015-04-01 10:00:00    0.0     
12 2015-04-01 11:00:00    0.0     
13 2015-04-01 12:00:00    0.0     
14 2015-04-01 13:00:00    0.0     
15 2015-04-01 14:00:00    0.0     
16 2015-04-01 15:00:00    0.0     
17 2015-04-01 16:00:00    0.0     
18 2015-04-01 17:00:00    0.0     
19 2015-04-01 18:00:00    0.0     
20 2015-04-01 19:00:00    0.0     
21 2015-04-01 20:00:00    0.0     
22 2015-04-01 21:00:00    0.0     
23 2015-04-01 22:00:00    1.6     
24 2015-04-01 23:00:00    0.2     
25 2015-04-02 00:00:00    1.5     
26 2015-04-02 01:00:00    1.5     
27 2015-04-02 02:00:00    0.5     
28 2015-04-02 03:00:00    0.0     
29 2015-04-02 04:00:00    0.0     
30 2015-04-02 05:00:00    0.0     
31 2015-04-02 06:00:00    0.0     
32 2015-04-02 07:00:00    0.5     
33 2015-04-02 08:00:00    0.3     
34 2015-04-02 09:00:00    0.0     
35 2015-04-02 10:00:00    0.0     
36 2015-04-02 11:00:00    0.0     
37 2015-04-02 12:00:00    0.0     
38 2015-04-02 13:00:00    0.0     
39 2015-04-02 14:00:00    0.0     
40 2015-04-02 15:00:00    0.0     
41 2015-04-02 16:00:00    0.0     
42 2015-04-02 17:00:00    0.0     
43 2015-04-02 18:00:00    0.0     
44 2015-04-02 19:00:00    0.0     
45 2015-04-02 20:00:00    0.0     
46 2015-04-02 21:00:00    0.0     
47 2015-04-02 22:00:00    0.0     
48 2015-04-02 23:00:00    0.0 
....
</code></pre>

<p>The output should be very close to:</p>

<pre><code>YYYY-MM-DD hh:mm:ss  YYYY-MM-DD hh:mm:ss  YYYY-MM-DD hh:mm:ss  YYYY-MM-DD hh:mm:ss
2015-04-01 00:00:00  2015-04-01 06:00:00  2015-04-01 12:00:00  2015-04-01 18:00:00
2015-04-02 00:00:00  2015-04-02 06:00:00  2015-04-02 12:00:00  2015-04-02 18:00:00 
.....
</code></pre>

<p>I appreciate your thoughts on this.</p>

<p><strong>EDIT</strong></p>

<p>How to implement @r2evans answer on a list object such as:</p>

<pre><code> x = runif(5856)
    flst1=list(x,x,x,x)

    flst1=lapply(flst1, function(x){x$datetime &lt;- as.POSIXct(x$datetime, tz = "GMT"); x})

    sixhours1=lapply(flst1, function(x) {x$bin &lt;- cut(x$datetime,sixhours);x})

    head(sixhours1[[1]],n=7)

    ret=lapply(sixhours1, function(x) aggregate(x$precip, list(x$bin), sum,na.rm=T))

    head(ret[[1]],n=20)
</code></pre>