Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounding numbers and forcing R to display 0's

I am computing values, which are essentially decimal values and am also supposed to report them in a table. For the sake of neat presentation, how can I round all the values to 3 decimal places and even force R to add the 0's for the values with less that 3 decimal places? Here is a more practical example:

# 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
like image 768
SavedByJESUS Avatar asked Dec 19 '25 20:12

SavedByJESUS


1 Answers

Here is my approach:

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

> results
[1] 1.259 3.200 2.840
like image 168
jazzurro Avatar answered Dec 21 '25 09:12

jazzurro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!