# Formatting numbers for printing in R: rounding and trailing zeroes

**Posted:**January 11, 2011

**Filed under:**code, data analysis, R 2 Comments

In journal articles, descriptive statistics results are often presented as a table of means, with standard deviation/standard error given in parentheses following the mean. Here is how you can prepare the print formatting when working in `R`

: it’s rather trivial, but I wasted a sufficient amount of time on it that I thought it was worth mentioning. (I follow this by using `latex()`

from the `Hmisc`

package to generate `LaTeX`

output of such a table.)

Suppose I have a vector of means for percent correct from 5 different experimental conditions, and a vector of the corresponding standard errors, and both are sorted in the same order:

all.res.corr$per.correct # Vector of mean percent correct, comes from a data frame called all.res.corr #[1] 52.53561 60.51282 64.13105 66.38177 67.46439

all.res.corr$se.per.corr # Vector of standard errors, from same data frame #[1] 2.409955 2.763525 2.831450 2.909737 2.902924

- First, use
`round()`

to round the numbers to two decimal places.round(all.res.corr$per.correct,2) #[1] 52.54 60.51 64.13 66.38 67.46 round(all.res.corr$se.per.corr,2) #[1] 2.41 2.76 2.83 2.91 2.90

- Then use
`formatC()`

to access C-like formatting options to format the two vectors to print up to 2 digits after the decimal point. The argument specification`format = "f"`

allows you to set the number of digits after the decimal point to print. This is a crucial step! If you don’t do it, you won’t get trailing zeroes to print, even though it looks like in the preceding code block, for instance, that`round()`

preserves the trailing zero. It gets lost in a numeric to character data type conversion. See this post for an alternative using`sprintf()`

(which may be familiar from many other programming languages).formatC(round(all.res.corr$per.correct,2), 2, format = "f" #[1] "52.54" "60.51" "64.13" "66.38" "67.46" # note conversion to character type formatC(round(all.res.corr$se.per.corr,2), 2, format = "f") [1] "2.41" "2.76" "2.83" "2.91" "2.90"

- Finally, use
`paste()`

to concatenate the vectors for publication, together with parentheses in the appropriate place. The code below concatenates the arguments given: the formatted mean vector`all.res.corr$per.correct`

, a space and opening parentheses`(`

, the formatted SE vector`all.res.corr$se.per.corr`

, and a closing parentheses`)`

. The separator`sep`

is specified to be an empty string, for there to be no separator between the arguments concatenated.

(per.all.res.corr.print &lt;- paste(formatC(round(all.res.corr$per.correct,2), 2, format = "f"), " (", formatC(round(all.res.corr$se.per.corr,2), 2, format = "f"), ")", sep="") # n.b. Parentheses around an assignment command print the assigned value #[1] "52.54 (2.41)" "60.51 (2.76)" "64.13 (2.83)" "66.38 (2.91)" "67.46 (2.90)"

Advertisements

Thank you for sharing your work on this problem!

This was very helpful!! Thank you.