Use int64 nanotimes as array dimnames in R

I have some lovely nanotimes:

> mynames
[1] "2020-04-15T00:29:00.000000000+00:00" "2020-04-15T00:33:00.000000000+00:00"

Which I'd like to add as dimnames to my array:

> a
     [,1] [,2]
[1,]   NA   NA
[2,]   NA   NA

However, they seem to be converted into rolled over strings:

> dimnames(a)[1] <- list(values=mynames)
> a
                      [,1] [,2]
1.39309697650764e-202   NA   NA
1.39315136717363e-202   NA   NA

> dimnames(a)
[1] "1.39309697650764e-202" "1.39315136717363e-202"

How can I keep my dimname assignment as nanotime/integer64s? Or if they are forced to character to be dimnames, how can I at least keep them from being strings of incorrect numbers?

You can see for example that these fail to round-trip back to suitable times:

> nanotime(dimnames(a)[1][[1]][1])
Error in RcppCCTZ::parseDouble(x, fmt = format, tz = tz) : 
  Parse error on 1.39309697650764e-202

1 answer

  • answered 2020-05-30 09:23 starja

    The documentation of dimnames states that the value is coerced to character. To do all the coercing back and from to nanotime which is based on integer64, you can first coerce your nanotime to integer64 and then to character. Then the back coercion works:

    mynames <- c(nanotime("2020-04-15T00:29:00.000000000+00:00"),
    a <- array(NA, dim = c(2, 2))
    # nanotime -> integer64 -> charachter
    dimnames(a)[1] <- list(values = as.character(as.integer64(mynames)))
    # character -> integer64 -> nanotime
    [1] "2020-04-15T00:29:00.000000000+00:00"