【问题标题】:Convert character base scientific number into number and preserving the scientific notation in R将字符基科学数转换为数字并保留 R 中的科学记数法
【发布时间】:2017-10-20 11:10:21
【问题描述】:

我在性格上有这个价值:

 val <- "1e-14105"

我想做的是将它们转换为数字并保留科学记数法。所以结果只是1e-14105 作为数字。

我试过了,但失败了:

> as.numeric(val)
[1] 0
> format(as.numeric(val), scientific=TRUE)
[1] "0e+00"

正确的做法是什么?

【问题讨论】:

    标签: r


    【解决方案1】:

    问题是浮点问题和 R 工作的准确度。 正在将 1e-14105 转换为数字,然后将其逼近为零。见The R Inferno: Circle 1, Falling into the Floating Point Trap

    > as.numeric("1e-14105")
    [1] 0
    > class(as.numeric("1e-14105"))
    [1] "numeric"
    > 1e-14105
    [1] 0
    

    【讨论】:

    猜你喜欢
    • 2017-11-27
    • 2021-12-23
    • 1970-01-01
    • 2019-04-22
    • 1970-01-01
    • 2022-10-06
    • 2019-04-21
    • 2015-07-03
    • 2017-04-01
    相关资源
    最近更新 更多