Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In R, how to stop xtable from automatically rounding?

How do I 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 can't find any option in the docs of xtable to turn it off.

like image 230
malana Avatar asked Dec 13 '22 08:12

malana


2 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}
like image 146
James Avatar answered Jan 04 '23 01:01

James


You can do it by transforming all columns into string, although it could 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"
like image 43
marcor92 Avatar answered Jan 04 '23 02:01

marcor92