Mona Jalal
Mona Jalal

Reputation: 38155

How detailed can we report time in R?

so when I use

>strptime(paste(as.character(as.POSIXct(as.numeric(substr(1438293919327731275,1,10)),origin="1970-01-01")),
+                substr(1438293919327731275,11,13),sep="."),"%Y-%m-%d %H:%M:%OS")
[1] "2015-07-30 17:05:19"

I want to know what happens to substr(14-19)? anyway we could report them say when we are using this number for a plot?

Upvotes: 0

Views: 118

Answers (2)

mra68
mra68

Reputation: 2960

As Joshua Ulrich already said, the fractional part of the seconds is not lost, but not displayed by default. If you want to see the fractional part, set the option "digit.secs" to the desired number of decimal places. But at least on my system I couldn't get more than 7 decimal places:

#============================================================
# With option "digit.secs"=NULL

options(digits.secs=NULL)

t1 <- as.POSIXct(as.numeric(substr(1438293919327731275,1,10)),
                 origin="1970-01-01")

t2 <- strptime(paste(as.character(t),
                     substr(1438293919327731275,11,13),
                     sep="."),
               "%Y-%m-%d %H:%M:%OS")

t3 <- strptime(paste(as.character(t),
                     substr(1438293919327731275,11,19),
                     sep="."),
               "%Y-%m-%d %H:%M:%OS")

t1
t2
t3

t2-t1
t3-t1

#==============================================================
# With option "digit.secs"=9

options(digits.secs=9)

t1 <- as.POSIXct(as.numeric(substr(1438293919327731275,1,10)),
                 origin="1970-01-01")

t2 <- strptime(paste(as.character(t),
                     substr(1438293919327731275,11,13),
                     sep="."),
               "%Y-%m-%d %H:%M:%OS")

t3 <- strptime(paste(as.character(t),
                     substr(1438293919327731275,11,19),
                     sep="."),
               "%Y-%m-%d %H:%M:%OS")

t1
t2
t3

t2-t1
t3-t1

Result:

> #============================================================
> # With option "digit.secs"=NULL
> 
> options(digits.secs=NULL)

> t1 <- as.POSIXct(as.numeric(substr(1438293919327731275,1,10)),
+                  origin="1970-01-01")

> t2 <- strptime(paste(as.character(t),
+                      substr(1438293919327731275,11,13),
+                      sep="."),
+                "% ..." ... [TRUNCATED] 

> t3 <- strptime(paste(as.character(t),
+                      substr(1438293919327731275,11,19),
+                      sep="."),
+                "% ..." ... [TRUNCATED] 

> t1
[1] "2015-07-31 00:05:19 CEST"

> t2
[1] "2015-07-31 00:05:19 CEST"

> t3
[1] "2015-07-31 00:05:19 CEST"

> t2-t1
Time difference of 0.3269999 secs

> t3-t1
Time difference of 0.3277311 secs

> #==============================================================
> # With option "digit.secs"=9
> 
> options(digits.secs=9)

> t1 <- as.POSIXct(as.numeric(substr(1438293919327731275,1,10)),
+                  origin="1970-01-01")

> t2 <- strptime(paste(as.character(t),
+                      substr(1438293919327731275,11,13),
+                      sep="."),
+                "% ..." ... [TRUNCATED] 

> t3 <- strptime(paste(as.character(t),
+                      substr(1438293919327731275,11,19),
+                      sep="."),
+                "% ..." ... [TRUNCATED] 

> t1
[1] "2015-07-31 00:05:19 CEST"

> t2
[1] "2015-07-31 00:05:19.327 CEST"

> t3
[1] "2015-07-31 00:05:19.327731 CEST"

> t2-t1
Time difference of 0.3269999 secs

> t3-t1
Time difference of 0.3277311 secs

Upvotes: 2

thelatemail
thelatemail

Reputation: 93813

To answer your query, yes, R plots can deal with POSIXct times that include sub-second fractions:

x <- 1:2
y <- as.POSIXct(c(1438293919.3277, 1438293919.4682),origin="1970-01-01")
#[1] "2015-07-31 08:05:19 EST" "2015-07-31 08:05:19 EST"
plot(y ~ x, yaxt="n")
axis.POSIXct(
  2,
  at=as.POSIXct(axTicks(2),origin="1970-01-01"), format="%OS2",
  las=2, cex.axis=0.7
)

enter image description here

Upvotes: 2

Related Questions