title: How to aggregate/ sum values by time in r
body: <p>There are 2 issues:</p>
  
  <ol>
  <li>I have time data in factor format and I want to change it into date format for later manipulation.</li>
  <li>The goal is to sum values of precipitation of the same time unit, eg. precipitation per hour. </li>
  </ol>
  
  <p>I tried to convert the time using <code>as.POSIXct()</code> or <code>as.date()</code> in <code>lubridate</code> but always get <code>NA</code> values after defining the format. This is the code I used:</p>
  
  <pre><code>tt=as.POSIXct(FixUseNew$StartTimestamp, )
  df$time &lt;- as.Date(df$time, "%d-%m-%Y")
  
  If I leave out the format and do the following : 
  tt=as.POSIXct(df$time)
  
  tt 
  
  hour(tt)
  </code></pre>
  
  <p>The date data looks like this now: <code>"0010-07-14 00:38:00 LMT"</code></p>
  
  <p>I wanted to use <code>aggregate</code> function to sum the precipitation in the same hour interval or day but couldn't do it as I am stuck with the date format.</p>
  
  <p>Just a brain dump. I was going to change the factor date in to character then to date format as following. Please advise if that is a good idea. </p>
  
  <pre><code>df$time &lt;-paste(substr(df$time,6,7),
                                substr(df$time,9,10), 
                                substr(df$time,1,4),sep="/")
  </code></pre>
  
  <p>Here is a subset of the data, hope this helps to illustrate the question better: </p>
  
  <pre><code>Id &lt;- c(1,2,3,4)
  Time &lt;- c("10/7/2014  12:30:00 am", "10/7/2014  01:00:05 am","10/7/2014  01:30:10 am", "10/7/2014  02:00:15 am")
  Precipitation &lt;- c(0.06, 0.02,0,0.25)
  cbind(Id, Time, Precipitation)
  </code></pre>
  
  <p>Thank you so much.</p>
  
  <p>Here is the outcome:
  It seems like the order is distorted:</p>
  
  <pre><code>6      1/1/15 0:35         602
  7      1/1/15 0:36         582
  8      1/1/15 0:37         958
  9      1/1/15 0:38         872
  10    1/10/14 0:31         500
  11    1/10/14 0:32         571
  12    1/10/14 0:33         487
  13    1/10/14 0:34         220
  14    1/10/14 0:35         550
  15    1/10/14 0:36         582
  16    1/10/14 0:37         524
  17    1/10/14 0:38         487
  
             ⋮
  
  106 10/10/14 15:16         494
  107  10/10/14 7:53          37
  108  10/10/14 7:56          24
  109  10/10/14 8:01           3
  110  10/11/14 0:30         686
  111  10/11/14 0:31         592
  112  10/11/14 0:32         368
  113  10/11/14 0:33         702
  114  10/11/14 0:34         540
  115  10/11/14 0:35         564
  </code></pre>