我正在计算值,这些值本质上是十进制值,并且还应该在表格中报告它们。为了简洁起见,我如何将所有值四舍五入到小数点后 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
最佳答案
这是我的方法:
results <- c(1.25854, 3.2, 2.84)
options(digits=4)
> results
[1] 1.259 3.200 2.840
关于舍入数字并强制 R 显示 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26448050/