Rstudio 和 R 终端给出不同的输出

标签 r unique rstudio

在 Rstudio(使用 R 3.1.1)中运行时,

length(unique(sort(c(outer(2:100,2:100,"^")))))
# 9220 

在 R 3.1.1 中,当我运行它时,
length(unique(sort(c(outer(2:100,2:100,"^")))))
# 9183

(正确的输出是 9183)

我不明白为什么......非常感谢帮助

最佳答案

正如 David Arenburg 所指出的,这是 32 位和 64 位 R 版本之间的区别,至少在 Windows 机器上是这样。据推测,涉及某种舍入误差。有趣的是,32 位 R 得到了正确的答案,而 64 位 R 找到了太多的唯一数字。

先确认一下9183确实是正确答案,我用了gmp包(C 多精度算术库 GMP 的包装器),它提供不受舍入误差影响的结果:

library(gmp)
x <- as.bigz(2:100)
length(unique(do.call(c, sapply(x, function(X) x^X))))
[1] 9183

这是我的 32 位 R 的结果:
length(unique(sort(c(outer(2:100,2:100,"^")))))
# [1] 9183
R.version[1:7]               _                           
# platform       i386-w64-mingw32            
# arch           i386                        
# os             mingw32                     
# system         i386, mingw32               
# status                                     
# major          3                           
# minor          1.2                         

这是我的 64 位 R 的结果:
length(unique(sort(c(outer(2:100,2:100,"^")))))
# [1] 9220
R.version[1:7]
# platform       x86_64-w64-mingw32          
# arch           x86_64                      
# os             mingw32                     
# system         x86_64, mingw32             
# status                                     
# major          3                           
# minor          1.2                         

关于Rstudio 和 R 终端给出不同的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27258084/

相关文章:

r - 在 R-studio 中使用 Git : cannot stage modified code files

r - 中值()的奇怪行为?

r - Plotly:如何为 map 设置 ylim 和 xlim?

r - 如何在不使用 R 中的循环的情况下计算一天的最大平均小时值

google-sheets - Google Sheets 公式用于计算具有不同开始/结束日期、重叠和间隙的任务的实际总持续时间

mysql - SQL UPDATE 语句用另一个没有唯一标识符的表的值更新一个表

r - "more"类似于 R 控制台中的命令

r - 一个或多个多边形覆盖的栅格单元的一部分 : is there a faster way to do this (in R)?

arrays - 在 Ruby 中生成大量独特的 token

r - 基于 2 个其他列查找一列的平均值 RStudio