R中将integer64转换为datatime的问题?

时间:2019-03-07 14:47:53

标签: r datetime unix-timestamp lubridate

给出以下dataframe Unix纪元中的以下integer64

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))
  

as.POSIXct.default(time_stamp)中的错误:不知道如何转换   “ time_stamp”分类为“ POSIXct”

请建议如何处理integer64个时代。

2 个答案:

答案 0 :(得分:1)

请原谅直接的语言,但您的问题毫无道理。取数据集中的第一个元素: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支持此处使用的integer64包的bit64类型的软件包。以示例为基础:

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点之后。)

答案 1 :(得分:0)

在这种情况下,错误是描述性的。 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年之前花了一些时间),则可以转换为整数。