In R, how to stop xtable from auto-rounding?

How to disable automatic rounding in this case?

> x <- c(2.222, 3.333, 6.6666) > df <- data.frame(x) > df x 1 2.2220 2 3.3330 3 6.6666 > xtable(df) 

Results in

 % latex table generated in R 2.11.1 by xtable 1.5-6 package % Tue Oct 25 12:13:08 2011 \begin{table}[ht] \begin{center} \begin{tabular}{rr} \hline & x \\ \hline 1 & 2.22 \\ 2 & 3.33 \\ 3 & 6.67 \\ \hline \end{tabular} \end{center} \end{table} 

I cannot find any option in the docs from xtable to disable it.

+4
source share
4 answers

How about digits ?

 xtable(df,digits=4) % latex table generated in R 2.12.2 by xtable 1.5-6 package % Tue Oct 25 11:39:25 2011 \begin{table}[ht] \begin{center} \begin{tabular}{rr} \hline & x \\ \hline 1 & 2.2220 \\ 2 & 3.3330 \\ 3 & 6.6666 \\ \hline \end{tabular} \end{center} \end{table} 
+11
source

Pay attention to @James answer for the correct answer (I didn’t even check that, as I believed, @mmmasterluke really read the docs).

Alternatively, you can use toLatex from the memisc package:

 library(memisc) x <- c(2.222, 3.333, 6.6666) df <- data.frame(x) toLatex(df, digits=4) 

gives you

 \begin{tabular}{D{.}{.}{4}} \toprule \multicolumn{1}{c}{x} \\ \midrule 2.2220 \\ 3.3330 \\ 6.6666 \\ \bottomrule \end{tabular} 

And it has many other parameters that you can use to configure latex output.

0
source

If you have already formatted the data frame with the preferred rounding and do not want to re-specify the numbers for each column, one of them is to turn everything into text. I just defined a function, which I then use elsewhere instead of xtable :

 myxtable <- function(x, ...) xtable(apply(x, 2, as.character), ...) 

Then the return value of myxtable can be used wherever you use the return value of xtable , but with formatting intact.

0
source

You can do this by converting all the columns to a row, although it may generate some warning message:

 > xtable(df, display=rep("s",ncol(df)+1)) % latex table generated in R 3.3.3 by xtable 1.8-2 package % Tue Oct 24 12:43:58 2017 \begin{table}[ht] \centering \begin{tabular}{rr} \hline & x \\ \hline 1 & 2.222 \\ 2 & 3.333 \\ 3 & 6.6666 \\ \hline \end{tabular} \end{table} Warning message: In formatC(x = c(2.222, 3.333, 6.6666), format = "s", digits = 2, : trasformo l'argomento in "character" in format="s" 
0
source

All Articles