[英]Force zeros with percent formatting
I'd like to display a dataframe feature in percentages with 4 decimal places:我想以小数点后 4 位的百分比显示 dataframe 功能:
scales::percent(1:3/12345)
"0.0081%" "0.0162%" "0.0243%"
This shows each value as a percent to 4 decimal places.这将每个值显示为小数点后 4 位的百分比。
But if I try eg但是如果我尝试例如
scales::percent(c(1:3/12345, 0.9), accuracy = 4)
[1] "0%" "0%" "0%" "88%"
I lose the values for the first 3. I'd like those to show as我失去了前 3 个的值。我希望那些显示为
"0.0081%", "0.0162%" "0.0243%".
How can I force the same number of digits while formatting as percent?如何在格式化为百分比时强制使用相同的位数? I always want 4 digits to the right of the decimal, even if they are all zero.
我总是想要小数点右边的 4 位数字,即使它们都为零。
You can do:你可以做:
scales::percent(c(1:3/12345, 0.9), accuracy = 0.0001)
[1] "0.0081%" "0.0162%" "0.0243%" "90.0000%"
The accuracy
argument has a rather counterintuitive functioning, meaning that if you want to have an output with more decimal places, you need to use a number smaller than 1 (which is also the default value). accuracy
参数有一个相当违反直觉的功能,这意味着如果您想要一个具有更多小数位的 output,您需要使用小于 1 的数字(这也是默认值)。 Every decimal place in the accuracy
argument then represents a decimal place in the output. accuracy
参数中的每个小数位都代表 output 中的小数位。
To illustrate the function at greater depth.为了更深入地说明 function。 If you want an output with one decimal place:
如果您想要一个小数点后一位的 output:
scales::percent(c(1:3/12345, 0.9), accuracy = 0.1)
[1] "0.0%" "0.0%" "0.0%" "90.0%"
while if want it with three decimal places:而如果想要保留三位小数:
scales::percent(c(1:3/12345, 0.9), accuracy = 0.001)
[1] "0.008%" "0.016%" "0.024%" "90.000%"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.