将 R 中的数字四舍五入到指定的位数 [英] Rounding numbers in R to specified number of digits

查看:59
本文介绍了将 R 中的数字四舍五入到指定的位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 R 中舍入数字时遇到问题.我有以下数据,我想将它们舍入为 8 位十进制数字.

I have a problem in rounding numbers in R. I have the following data, and I want to round them to 8 decimal digits.

structure(c(9.50863385275955e-05, 4.05702267762077e-06, 2.78921491976249e-05, 
8.9107773737659e-05, 5.0672643927135e-06, 5.87776809485182e-05,
2.76421630542694e-05, 5.51662570625727e-05, 2.52790624570593e-05, 
2.00407457671806e-05, 8.33373482160056e-05, 7.8297940825207e-05,
2.00407457671806e-05, 8.33373482160056e-05, 7.8297940825207e-05,
2.00407457671806e-05, 8.33373482160056e-05, 7.8297940825207e-05
), .Names = c("CC/CC", "TT/CC", "CT/CC", "NC/CC", "CC/TT", "TT/TT",
"CT/TT", "NC/TT", "CC/CT", "TT/CT", "CT/CT", "NC/CT"))

这些数字是指数形式,我想将它们转换为小数点后 8 位的有理数.

These digits are in the form of exponent and I want to convert them to rational numbers with 8 decimal places.

推荐答案

为了更好地控制数字格式,请尝试 formatC()

For fine control over formatting of numbers, try formatC()

res <- structure(c(9.50863385275955e-05, 4.05702267762077e-06), 
                 .Names = c("CC/CC", "TT/CC"))

formatC(res, format="f", digits=8)

这篇关于将 R 中的数字四舍五入到指定的位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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