title: Convert data frame with epoch timestamps to time-series with milliseconds in R
body: <p>I have the following data.frame:</p>
  
  <pre><code>df &lt;- data.frame(timestamp=c(1428319770511, 1428319797218, 1428319798182, 1428319803327, 1428319808478),
                   session=c("A","A","B","A","A"))
  </code></pre>
  
  <p>I'd like to convert this data frame to a time series and work on time windows shorter than one second. I already tried <code>zoo</code> and <code>xts</code>, but I found it difficult to represent the epoch times as dates.
  Here's what I already tried:</p>
  
  <pre><code>df$date&lt;-strptime(as.POSIXct(df$timestamp, origin="1970-01-01"),format="%Y-%m-%d %H:%M:%OS")
  </code></pre>
  
  <p>Which return NAs. Calling this:</p>
  
  <pre><code>df$date&lt;-strptime(as.POSIXct(df$timestamp/1000, origin="1970-01-01"),format="%Y-%m-%d %H:%M:%OS")
  </code></pre>
  
  <p>Works but doesn't contain milliseconds data.
  I also tried to play with <code>options(digits.secs=3)</code> but with no luck.</p>
  
  <p>I guess I'm hitting a small wall here with R's handling of milliseconds but any ideas would be greatly appreciated.</p>
  
  <p>---EDIT---</p>
  
  <p>Ok, Thanks to Joshua's answer and a comment here <a href="https://stackoverflow.com/questions/13456241/convert-unix-epoch-to-date-object-in-r">Convert UNIX epoch to Date object in R</a> by @Dirk Eddelbuettel, dividing by 1000 doesn't truncate the data. So this works:</p>
  
  <pre><code>options(digits.secs = 3)
  df$date&lt;-as.POSIXct(df$timestamp/1000, origin="1970-01-01", tz="UTC")
  </code></pre>
  
  <p>Which returns:</p>
  
  <pre><code>timestamp       session date    
  1428319770511   A       2015-04-06 14:29:30.510
  1428319797218   A       2015-04-06 14:29:57.217
  1428319798182   B       2015-04-06 14:29:58.181
  1428319803327   A       2015-04-06 14:30:03.326
  1428319808478   A       2015-04-06 14:30:08.477
  </code></pre>