R问题中的integer64到datetime转换?

integer64 to datatime conversion in R issue?

鉴于 integer64 unix 纪元的以下 dataframe

data_df <- structure(list(time_stamp = structure(c(0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000282505613660396, 
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000282505613660396, 
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000282505613660396, 
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000282505613660396, 
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000282505613660396, 
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000282505613660396, 
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000282505613660396, 
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000282505613660396, 
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000282505613660396, 
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000282505613660396
), class = "integer64")), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -10L))

我想将其转换为日期时间(as.POSIXctanytime()),但出现错误:

    data_df %>%
  dplyr::select(time_stamp) %>% 
  head(10) %>%
  dplyr::mutate(dt = anytime(time_stamp)) %>% dput()

给出:

structure(list(time_stamp = structure(c(0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000282505613660396, 
    0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000282505613660396, 
    0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000282505613660396, 
    0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000282505613660396, 
    0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000282505613660396, 
    0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000282505613660396, 
    0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000282505613660396, 
    0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000282505613660396, 
    0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000282505613660396, 
    0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000282505613660396
    ), class = "integer64"), dt = structure(c(0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000282505613660396, 
    0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000282505613660396, 
    0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000282505613660396, 
    0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000282505613660396, 
    0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000282505613660396, 
    0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000282505613660396, 
    0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000282505613660396, 
    0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000282505613660396, 
    0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000282505613660396, 
    0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000282505613660396
    ), class = c("POSIXct", "POSIXt"), tzone = "Etc/UTC")), class = c("tbl_df", 
    "tbl", "data.frame"), row.names = c(NA, -10L))

data_df %>%
  dplyr::select(time_stamp) %>% 
  head(10) %>%
  dplyr::mutate(dt = as.POSIXct(time_stamp))

Error in as.POSIXct.default(time_stamp) : do not know how to convert 'time_stamp' to class “POSIXct”

请指教如何处理integer64纪元。

请原谅直接的语言,但你的问题没有意义。获取数据集的第一个元素:0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000282505613660396。这在您列出的数据类型的 any 中根本无法表示。包括integer64。句号。

现在,碰巧我的 nanotime 以最佳可用分辨率 执行此操作,即以 64 位整数表示的纳秒。并且 64 位整数允许自纪元以来的纳秒增量,精度约为 19 位。不是您要求的 100 多个数字。没有(小内存)变量可以。

至于nanotimeexample()显示了一些用途,包括解析:

R> library(nanotime)
R> example(nanotime)

nanotmR> x <- nanotime("1970-01-01T00:00:00.000000001+00:00")

nanotmR> print(x)
[1] "1970-01-01T00:00:00.000000001+00:00"

nanotmR> x <- x + 1

nanotmR> print(x)
[1] "1970-01-01T00:00:00.000000002+00:00"

nanotmR> format(x)
[1] "1970-01-01T00:00:00.000000002+00:00"

nanotmR> x <- x + 10

nanotmR> print(x)
[1] "1970-01-01T00:00:00.000000012+00:00"

nanotmR> format(x)
[1] "1970-01-01T00:00:00.000000012+00:00"

nanotmR> format(nanotime(Sys.time()) + 1:3)  # three elements each 1 ns apart
[1] "2019-03-10T20:06:53.534292001+00:00" "2019-03-10T20:06:53.534292002+00:00" 
[3] "2019-03-10T20:06:53.534292003+00:00"
R> 

最重要的是,data.table 支持此处使用的 bit64 包的 integer64 类型。基于示例:

R> library(data.table)
data.table 1.12.0  Latest news: r-datatable.com
R> dt <- data.table(ns = nanotime(Sys.time()) + 1:3)
R> dt[]
                                    ns
1: 2019-03-10T20:08:48.165136001+00:00
2: 2019-03-10T20:08:48.165136002+00:00
3: 2019-03-10T20:08:48.165136003+00:00
R> dt[, pt := as.POSIXct(ns)]
R> dt[]
                                    ns                         pt
1: 2019-03-10T20:08:48.165136001+00:00 2019-03-10 15:08:48.165136
2: 2019-03-10T20:08:48.165136002+00:00 2019-03-10 15:08:48.165136
3: 2019-03-10T20:08:48.165136003+00:00 2019-03-10 15:08:48.165136
R> 

我将这种纳秒粒度的双重表示与 POSIXct 表示一起用于 R 用途,包括整天绘图。 (请注意,在 UTC 中显示 nanotime / integer64 列存在格式错误,但基础表示是合理且正确的,因为 pt 转换为 POSIXct 显示。它目前在我的时区下午 3 点刚过。)

在这种情况下,错误是描述性的。 as.POSIXct 不处理 integer64。这里有一些简单的命令,表明如此

library(bit64)

i <- 1
i64 <- as.integer64(i)

as.POSIXct(i, tz='UTC', origin='1970-01-01')

## You will get an error here
as.POSIXct(i64, tz='UTC', origin='1970-01-01')

如果您可以接受较低的精度(2038 年之前有一段时间),您可以转换为整数。