Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This question has gone on far too long -- especially considering that the question itself links to the answer.</p> <p>This is an output representation problem, and is well-known. Rounding time values has implications. You cannot just round the subsecond values without getting incorrect results in some cases.</p> <p>The solution is here (as linked in the question): <a href="https://stackoverflow.com/questions/7726034/how-r-formats-posixct-with-fractional-seconds">How R formats POSIXct with fractional seconds</a></p> <p>Stealing directly from Aaron's excellent answer:</p> <pre><code>myformat.POSIXct &lt;- function(x, digits=0) { x2 &lt;- round(unclass(x), digits) attributes(x2) &lt;- attributes(x) x &lt;- as.POSIXlt(x2) x$sec &lt;- round(x$sec, digits) format.POSIXlt(x, paste("%Y-%m-%d %H:%M:%OS",digits,sep="")) } x &lt;- as.POSIXct("2012-12-14 15:42:04.577895 EDT") </code></pre> <p>What you are attempting:</p> <pre><code>as.POSIXlt(as.POSIXct(sprintf("%s",(format(x, "%Y-%m-%d %H:%M:%OS6")))))$sec ## [1] 4.577894 </code></pre> <p>Aaron's code:</p> <pre><code>myformat.POSIXct(x, 6) ## [1] "2012-12-14 15:42:04.577895" </code></pre> <p>One might think to use <code>sprintf</code> with a format string of <code>%.06f</code> to format the subsecond value, but this will fail if the value is slightly below an integer:</p> <pre><code>sprintf('%.06f', .9999999) ## [1] "1.000000" </code></pre> <p>Converting to a POSIXlt causes the proper rollover into seconds, minutes, etc., if the subsecond value is rounded up.</p> <p>To show you that this is <strong>not</strong> a data problem:</p> <pre><code>y &lt;- as.numeric(x) y ## [1] 1355521324.5778949261 sprintf('%06f', y - floor(y)) ## [1] "0.577895" </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload