R 中小数位的格式
我有一个数字,例如 1.128347132904321674821,我想在输出到屏幕(或写入文件)时仅显示两位小数。如何做到这一点?
x <- 1.128347132904321674821
编辑:
使用:
options(digits=2)
已被建议作为可能的答案。有没有一种方法可以在脚本中指定它以供一次性使用?当我将它添加到我的脚本中时,它似乎没有做任何不同的事情,而且我对大量重新输入以格式化每个数字不感兴趣(我正在自动化一个非常大的报告)。
--
答案:round(x, 数字=2)
I have a number, for example 1.128347132904321674821 that I would like to show as only two decimal places when output to screen (or written to a file). How does one do that?
x <- 1.128347132904321674821
EDIT:
The use of:
options(digits=2)
Has been suggested as a possible answer. Is there a way to specify this within a script for one-time use? When I add it to my script it doesn't seem to do anything different and I'm not interested in a lot of re-typing to format each number (I'm automating a very large report).
--
Answer: round(x, digits=2)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
背景:本页建议的一些答案(例如,
signif
、options(digits=...)
)并不保证一定数量显示任意数字的小数位数。我认为这是 R 中的一个设计功能,良好的科学实践涉及根据“ 原则显示一定数量的数字重要数字”。然而,在许多领域(例如,APA 样式、业务报告)格式要求规定显示一定数量的小数位。这样做通常是为了一致性和标准化目的,而不是关心有效数字。解决方案:
以下代码精确显示数字
x
的两位小数。例如:
更通用的函数如下,其中
x
是数字,k
是要显示的小数位数。trimws
删除所有前导空格,如果您有数字向量,这会很有用。例如,
替代方案的讨论:
formatC 答案和sprintf 答案 效果相当好。但在某些情况下它们会显示负零,这可能是不需要的。即,
一种可能的解决方法如下:
Background: Some answers suggested on this page (e.g.,
signif
,options(digits=...)
) do not guarantee that a certain number of decimals are displayed for an arbitrary number. I presume this is a design feature in R whereby good scientific practice involves showing a certain number of digits based on principles of "significant figures". However, in many domains (e.g., APA style, business reports) formatting requirements dictate that a certain number of decimal places are displayed. This is often done for consistency and standardisation purposes rather than being concerned with significant figures.Solution:
The following code shows exactly two decimal places for the number
x
.For example:
A more general function is as follows where
x
is the number andk
is the number of decimals to show.trimws
removes any leading white space which can be useful if you have a vector of numbers.E.g.,
Discussion of alternatives:
The formatC answers and sprintf answers work fairly well. But they will show negative zeros in some cases which may be unwanted. I.e.,
One possible workaround to this is as follows:
您可以根据需要设置数字的格式,例如
x
,最多可达小数位。这里x
是一个有很多小数位的数字。假设我们希望显示该数字最多 8 位小数:这里
format="f"
给出了通常小数位的浮点数,例如 xxx.xxx 和digits
指定位数。相比之下,如果您想显示一个整数,则可以使用format="d"
(很像sprintf
)。You can format a number, say
x
, up to decimal places as you wish. Herex
is a number with many decimal places. Suppose we wish to show up to 8 decimal places of this number:Here
format="f"
gives floating numbers in the usual decimal places say, xxx.xxx, anddigits
specifies the number of digits. By contrast, if you wanted to get an integer to display you would useformat="d"
(much likesprintf
).你可以试试我的包formattable。
好处是,
x
仍然是一个数字向量,您可以使用相同的格式进行更多计算。更好的是,数字不会丢失,您可以随时使用更多数字重新格式化:)
You can try my package formattable.
The good thing is,
x
is still a numeric vector and you can do more calculations with the same formatting.Even better, the digits are not lost, you can reformat with more digits any time :)
对于 2 个小数位,假设您想要保留尾随零
,这给出了
正如 @mpag 下面提到的,似乎 R 有时可以使用此方法和舍入方法给出意外的值,例如 sprintf(5.5550, fmt='%#.2f') 给出 5.55 ,不是 5.56
for 2 decimal places assuming that you want to keep trailing zeros
which gives
As @mpag mentions below, it seems R can sometimes give unexpected values with this and the round method e.g. sprintf(5.5550, fmt='%#.2f') gives 5.55, not 5.56
类似的东西:
数字选项的定义:
Something like that :
Definition of digits option :
如果您更喜欢有效数字而不是固定数字,则使用 signif 命令可能有用:
If you prefer significant digits to fixed digits then, the signif command might be useful:
检查函数 prettyNum, format
是否有试验零(例如 123.1240),使用
sprintf(x, fmt='%#.4g')
Check functions prettyNum, format
to have trialling zeros (123.1240 for example) use
sprintf(x, fmt='%#.4g')
函数
formatC()
可用于将数字格式化为小数点后两位。即使结果值包含尾随零,该函数也会给出两位小数。The function
formatC()
can be used to format a number to two decimal places. Two decimal places are given by this function even when the resulting values include trailing zeros.我使用此变体强制打印 K 位小数:
I'm using this variant for force print K decimal places:
请注意,R 中的数字对象以双精度存储,这给你(大约)16 位小数精度的数字 - 其余的将是噪音。我承认上面显示的数字可能只是一个例子,但它有 22 位数字长。
Note that numeric objects in R are stored with double precision, which gives you (roughly) 16 decimal digits of precision - the rest will be noise. I grant that the number shown above is probably just for an example, but it is 22 digits long.
在我看来,就像
Per 一点 在线帮助。
Looks to me like to would be something like
Per a little online help.
如果您只想对数字或列表进行四舍五入,只需使用
然后,数据将四舍五入到小数点后两位。
if you just want to round a number or a list, simply use
Then, data will be round to 2 decimal place.
在这里,我将所有数值更改为只有 2 位小数。如果需要更改为更多的小数位数
将 k 替换为所需的小数位数
Here I am changing all numeric values to have only 2 decimal places. If you need to change it to more decimal places
Replace the k with the desired number of decimal places
我编写了这个可以改进的函数,但看起来在极端情况下效果很好。例如,在 0.9995 的情况下,投票正确答案给出的结果是 1.00,这是不正确的。我在数字没有小数的情况下使用该解决方案。
例子:
I wrote this function that could be improve but looks like works well in corner cases. For example, in the case of 0.9995 the vote correct answer gives us 1.00 which is incorrect. I use that solution in the case that the number has no decimals.
Example:
这是我从单位到百万的方法。
digits 参数让我调整有效值的最小数量(整数+小数)。您可以先调整内部的小数舍入。
here's my approach from units to millions.
digits parameter let me adjust the minimum number of significant values (integer + decimals). You could adjust decimal rounding inside first.