Formatting Decimal places in R

Tag: r Author: zlong2512 Date: 2010-07-24

I have a number, for example 1.128347132904321674821 that I would like to show as only two decimal places when output to screen (or written to a file). How does one do that?

x <- 1.128347132904321674821

EDIT:

The use of:

options(digits=2)

Has been suggested as a possible answer. Is there a way to specify this within a script for one-time use? When I add it to my script it doesn't seem to do anything different and I'm not interested in a lot of re-typing to format each number (I'm automating a very large report).

--

Answer: round(x, digits=2)

Related question:
haha, thanks Shane. Although, again, I should specify I was looking for something within R base.
any particular reason to stick within R base (not that it's a problem, I'm just curious)
It just seems like something that the base package should be able to handle. So I'd rather not poke around in new packages that I'm not familiar with for a simple problem :)
@PaulHurleyuk, I think it's good practice in programming to use the minimal number of libraries as possible. Someone who uses a different library for each trivial need usually ends up with a mess, big files, portability issues, etc.

Best Answer

Background: Some answers suggested on this page (e.g., signif, options(digits=...)) do not guaranty that a certain number of decimals are displayed for an arbitrary number. I presume this is a design feature in R whereby good scientific practice involves showing a certain number of digits based on principles of "significant figures". However, in many domains (e.g., APA style, business reports) formatting requirements dictate that a certain number of decimal places are displayed. This is often done for consistency and standardisation purposes rather than being concerned with significant figures.

Solution:

The following code shows exactly two decimal places for the number x.

format(round(x, 2), nsmall = 2)

For example:

> format(round(1.20, 2), nsmall = 2)
[1] "1.20"
> format(round(1, 2), nsmall = 2)
[1] "1.00"
> format(round(1.1234, 2), nsmall = 2)
[1] "1.12"

A more general function is as follows where x is the number and k is the number of decimals to show.

specify_decimal <- function(x, k) format(round(x, k), nsmall=k)

E.g.,

> specify_decimal(1234, 5)
[1] "1234.00000"
> specify_decimal(0.1234, 5)
[1] "0.12340"

comments:

+1 Only answer that worked for me, correctly prints 0.0001 as 0.00

Other Answer1

Well, the two that come to mind are

fixed(1.128347132904321674821, digits=2)

or if you prefer siginificant digits to fixed digits then;

signif(1.128347132904321674821,digits=3)

comments:

Thanks Paul. These two weren't exactly what I was looking for, but signif led me to round() which is exactly what I needed. Cheers,
Error: could not find function "fixed"
signif works for the specific number provided, but I presume the common applied problem is when you need to show exactly two decimal places but you don't know what the number is ahead of time. In that case, signif will give different numbers of decimals depending on the actual number.

Other Answer2

Note that numeric objects in R are stored with double precision, which gives you (roughly) 16 decimal digits of precision - the rest will be noise. I grant that the number shown above is probably just for an example, but it is 22 digits long.

comments:

Confirmed, it's just for an example. I mashed the keyboard.

Other Answer3

Looks to me like to would be something like

library(tutoR)
format(1.128347132904321674821, 2)

Per a little online help.

comments:

I found this, but it requires a package, I'm looking for something within the base package.
@brandon, format() is part of base. Open up R and type ?format ... no packages needed.
Hrmmm, did you look at what this outputs? [1] "1.128347" otherwise, you're quite right about it being in the base package, my bad.
Perhaps try format.default(x, digits = 2) just a shot in the dark though based on the link provided. That info is some what lacking from what I normally read for documentation, I expected to see the printed outputs as well.
Just noticed that your link points to tutoR documentation, which is not part of the base.

Other Answer4

The function formatC() can be used to format a number to two decimal places. Two decimal places are given by this function even when the resulting values include trailing zeros.

Other Answer5

Something like that :

options(digits=2)

Definition of digits option :

digits: controls the number of digits to print when printing numeric values.

comments:

Is there any way to set this dynamically when running a script?
This works for outputting within the R console, but doesn't work within my script (they still come out with .1289273982)

Other Answer6

Check functions prettyNum, format

to have trialling zeros (123.1240 for example) use sprintf(x, fmt='%#.4g')

comments:

ITYM ` ... fmt='%#.4g')

Other Answer7

I'm using this variant for force print K decimal places:

# format numeric value to K decimal places
formatDecimal <- function(x, k) format(round(x, k), trim=T, nsmall=k)