首页 > 解决方案 > 在 R 中使用 int64 nanotimes 作为数组 dimnames

问题描述

我有一些可爱的纳米时代:

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

我想将其作为暗名添加到我的数组中:

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

但是,它们似乎被转换为翻转字符串:

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

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

如何将我的 dimname 分配保持为 nanotime/integer64s?或者,如果他们被迫将其字符化为暗名,我怎样才能至少防止它们成为不正确的数字字符串?

例如,您可以看到这些无法返回到合适的时间:

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

标签: rarraysnanotime

解决方案


的状态的文档被强制为. 要做所有基于to的强制返回和from ,你可以先强制你的to ,然后再强制to 。然后反向强制起作用:dimnamesvaluecharacternanotimeinteger64nanotimeinteger64character

library(nanotime)

mynames <- c(nanotime("2020-04-15T00:29:00.000000000+00:00"),
             nanotime("2020-04-15T00:33: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
nanotime(as.integer64(dimnames(a)[1][[1]][1]))
[1] "2020-04-15T00:29:00.000000000+00:00"

推荐阅读