舍入数字并强制R显示0

时间:2014-10-19 06:43:37

标签: r decimal rounding

我正在计算值,它们本质上是十进制值,我也应该在表中报告它们。为了整洁的呈现,我如何将所有值舍入到3个小数位,甚至强制R为小于3位小数的值添加0?这是一个更实际的例子:

# The following vector contains all the values I computed (this is an example)
results <- c(1.25854, 3.2, 2.84)

# I am currently using the round() function, which does not give me the desired result
rounded_results <- round(results, 3)
rounded_results

> [1] 1.259 3.2 2.84

# The result I would like to obtain is

> [1] 1.259 3.200 2.840

1 个答案:

答案 0 :(得分:3)

这是我的方法:

results <- c(1.25854, 3.2, 2.84)
options(digits=4)

> results
[1] 1.259 3.200 2.840