【问题标题】:Internal representation of int NA [duplicate]int NA 的内部表示
【发布时间】:2019-06-08 15:34:14
【问题描述】:

这是关于 R 内部的问题。整数 NA 值如何在 R 中表示? 与浮动不同,没有神奇的位序列来表示 NaN。

# Create big array. newer versions of R won't allocate memory to store data
# Instead star/end values are stored internally
a <- 1:1e6 # 

# Change some random value. This will cause and array to be allocated
a[123] <- NA
typeof(a)

此时a仍然是一个整数数组。 a[123] 如何在内部表示? R 是否使用一些幻数来指示整数为 NA?

我对整数内部表示的主要兴趣与二进制读/写(readBin/writeBin)有关。使用外部源执行二进制 I/O 时如何处理 NA,例如通过套接字?

【问题讨论】:

标签: r


【解决方案1】:

R 使用最小整数值来表示 NA。在 4 字节系统上,有效整数值通常是 -2,147,483,648 到 2,147,483,647,但在 R

> .Machine$integer.max
[1] 2147483647
> -.Machine$integer.max
[1] -2147483647
> -.Machine$integer.max - 1L
[1] NA
Warning message:
In -.Machine$integer.max - 1L : NAs produced by integer overflow

还有,

> .Internal(inspect(NA_integer_))
@7fe69bbb79c0 13 INTSXP g0c1 [NAM(7)] (len=1, tl=0) -2147483648

【讨论】:

  • 我已经更新了我的问题,但我认为答案从您的帖子中显而易见
  • .Machine$integer.max + 1L 超出“通常”范围,因此被替换为 NA 值。
猜你喜欢
  • 2019-01-12
  • 1970-01-01
  • 2020-09-27
  • 1970-01-01
  • 2017-11-07
  • 1970-01-01
  • 1970-01-01
  • 2012-03-30
  • 1970-01-01
相关资源
最近更新 更多