3

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
SavedByJESUS
  • 3,262
  • 4
  • 32
  • 47
  • 1
    See this [site](http://www.dummies.com/how-to/content/how-to-format-numbers-in-r.html) –  Oct 19 '14 at 06:49

1 Answers1

3

Here is my approach:

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

> results
[1] 1.259 3.200 2.840
jazzurro
  • 23,179
  • 35
  • 66
  • 76
  • Will the use of `options()` not affect the behavior of R? – SavedByJESUS Oct 19 '14 at 06:51
  • 1
    @SavedByJESUS You can keep the present options first. `op <- options()`. Then you change `digits`. When you are done with your task, you can reset digit by `options(op)` – jazzurro Oct 19 '14 at 07:12