R seq function produces wrong results [closed]

徘徊边缘 提交于 2019-12-23 20:40:49

问题


It seems that one of my machines produces wrong results for seq function while another machine or the online r-fiddle (http://www.r-fiddle.org) interpreter give expected results. On the machine in question following happens:

seq(from = 1, to = 1.1, by = 0.01)
[1] 1.0 1.0 1.0 1.0 1.0 1.0 1.1 1.1 1.1 1.1 1.1

Changing the command slightly returns expected result

seq(from = 0.99, to = 1.1, by = 0.01)
[1] 0.99 1.00 1.01 1.02 1.03 1.04 1.05 1.06 1.07 1.08 1.09 1.10

As soon as I cross the "1" threshold, the wrong result occurs, e.g. same when I do from = 2.95 to = 3.1, etc. Not sure how to find an answer as I couldn't replicate the problem on my other machine or on r-fiddle. The problem persists even after restarting the pc.

R version 3.1.3 (2015-03-09)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=German_Germany.1252 LC_CTYPE=German_Germany.1252
[3] LC_MONETARY=German_Germany.1252 LC_NUMERIC=C
[5] LC_TIME=German_Germany.1252


回答1:


You have set the digits option too low:

options(digits=2)
seq(from = 1, to = 1.1, by = 0.01)
#[1] 1.0 1.0 1.0 1.0 1.0 1.1 1.1 1.1 1.1 1.1 1.1
options(digits=7)
seq(from = 1, to = 1.1, by = 0.01)
#[1] 1.00 1.01 1.02 1.03 1.04 1.05 1.06 1.07 1.08 1.09 1.10


来源:https://stackoverflow.com/questions/35994327/r-seq-function-produces-wrong-results

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!