R & 中的字符数据类型长度问题小数精度 [英] Issue with character data type length in R & decimal precision

查看:32
本文介绍了R & 中的字符数据类型长度问题小数精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试创建一个函数来获取数值数据的精度.(小数点右边的位数)

Trying to create a function to get the precision of numeric data. (the number of digits to the right of the decimal place)

    decimalplaces <- function(x) {
            if (x %% 1 != 0) {
                    pattern <- "^([0-9]+)[.]([0-9]+)$"
                    dec_part <- gsub(pattern,"\\2", x)            
                    nchar(dec_part)
            } else { 
                    return(0) 
        }
    }

超过 16 位的值会出现问题 -- nchar 将dec_part"强制转换为只能存储 16 位的字符串.

The issue occurs with values with more than 16 digits -- nchar coerces "dec_part" to a string which can only store 16 digits.

有没有办法克服 R 中的这个限制?

Is there a way to overcome this limitation in R?

对于数值数据,是否有 nchar 的替代方案?

Are there alternatives to nchar for numeric data?

(R 版本 3.1.1 64 位)

(R version 3.1.1 64 bit)

推荐答案

问题"不在 nchar 中,而是在 gsub 中,它应用 as.字符 到非字符 x.as.character 的文档说:

The 'problem' is not in nchar but in gsub, which applies as.character to a non-character x. The documentation for as.character says:

as.character 表示实数和复数到 15 位有效数字(从技术上讲是编译器对 ISO C 常量的设置DBL_DIG,在支持 IEC60559 算法的机器上为 15根据 C99 标准).这确保了所有数字结果将是可靠的(而不是表示的结果错误),但确实意味着转换为字符并返回数字可能会更改号码.如果要将数字转换为字符以最大可能的精度,使用格式.

as.character represents real and complex numbers to 15 significant digits (technically the compiler's setting of the ISO C constant DBL_DIG, which will be 15 on machines supporting IEC60559 arithmetic according to the C99 standard). This ensures that all the digits in the result will be reliable (and not the result of representation error), but does mean that conversion to character and back to numeric may change the number. If you want to convert numbers to character with the maximum possible precision, use format.

所以,你可以使用

dec_part <- gsub(pattern,"\\2", format(x,digits=22))    

代替

dec_part <- gsub(pattern,"\\2", x)

在您的代码中,但要小心,因为 15 位有效数字的限制是有充分理由的,因此很有可能在尾随数字中找到噪音.例如,

in your code, but be careful because the 15 significant digit limit was set for a good reason, so there is a good chance to find just noise in trailing numbers. For example,

> format(1/3,digits=22)
[1] "0.3333333333333333148296"

这篇关于R &amp; 中的字符数据类型长度问题小数精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆