How to specify decimal places not significant digits in xtable output

4.5k views Asked by At

I'm using the R xtable package to print out my dataframes as LaTeX tables. I have been using the digits option to specify the number of significant digits for the values in my tables. However, with digits=3, I get outputs like:

...
chat & chatd & 1.04 & -0.526 & 0.943 \\ 
dav1 & davis & 0.922 & 0.0551 & 0.374 \\ 
...

I'd like to be able to specify the number of decimal places in my tables, so that everything has 3 decimal places, even if the last few places are padded with zeros. So, for example, the first numeric column would have

1.040
0.922

so that everything has 3 decimal places, and therefore everything lines up nicely.

Is there a way to do this?

1

There are 1 answers

0
Henrik On
set.seed(123)
df <- data.frame(x = rnorm(2) + 10, y = rnorm(2) + 100, z = 0.1)
df

#          x        y   z
# 1 9.439524 101.5587 0.1
# 2 9.769823 100.0705 0.1

xtable(df, digits = 3)

# % latex table generated in R 3.0.1 by xtable 1.7-1 package
# [...snip...] 
# 1 & 9.440 & 101.559 & 0.100 \\ 
# 2 & 9.770 & 100.071 & 0.100 \\