jester
jester

Reputation: 309

The xtable package creating decimal places from nowhere?

I have a data frame looks like:

         Month alpha  beta cov_td Q_BAS T_BAS volume
1 January 2008  0.18  0.23  -0.49  0.31  0.23  70533
2     February  0.22  0.25  -0.38  0.33  0.25  48367
3        March  0.41  0.05  -0.38  0.35  0.25  70584
4        April  0.37  0.15  -0.30  0.35  0.25  46666
5          May  0.27  0.15  -0.45  0.34  0.24  72829
6         June  0.72 -0.22  -0.28  0.40  0.28  60437

When I try to make this a table from the Latex with the code:

xtable(a)

The xtable generated the following output:

% latex table generated in R 3.2.1 by xtable 1.7-4 package
% Wed Aug 05 22:04:58 2015
\begin{table}[ht]
\centering
\begin{tabular}{rlrrrrrr}
  \hline
 & Month & alpha & beta & cov\_td & Q\_BAS & T\_BAS & volume \\ 
  \hline
1 & January 2008 & 0.18 & 0.23 & -0.49 & 0.31 & 0.23 & 70532.85 \\ 
  2 & February & 0.22 & 0.25 & -0.38 & 0.33 & 0.25 & 48367.30 \\ 
  3 & March & 0.41 & 0.05 & -0.38 & 0.35 & 0.25 & 70583.80 \\ 
  4 & April & 0.37 & 0.15 & -0.30 & 0.35 & 0.25 & 46665.68 \\ 
  5 & May & 0.27 & 0.15 & -0.45 & 0.34 & 0.24 & 72829.43 \\ 
  6 & June & 0.72 & -0.22 & -0.28 & 0.40 & 0.28 & 60437.14 \\ 

In my df, the last column are purely integers, but i am not sure why the xtable package create me two decimal places (from no where) for my integers.

The entire table is big so I can not manually correct them.

Upvotes: 0

Views: 748

Answers (1)

vaettchen
vaettchen

Reputation: 7659

Try

xtable( a, digits = c( 0, 0, 2, 2, 2, 2, 2, 0 ) )

% latex table generated in R 3.2.1 by xtable 1.7-4 package
% Thu Aug  6 08:36:53 2015
\begin{table}[ht]
\centering
\begin{tabular}{rlrrrrrr}
  \hline
  & Month & alpha & beta & cov\_td & Q\_BAS & T\_BAS & volume \\ 
  \hline
  1 & January 2008 & 0.18 & 0.23 & -0.49 & 0.31 & 0.23 & 70533 \\ 
  2 & February & 0.22 & 0.25 & -0.38 & 0.33 & 0.25 & 48367 \\ 
  3 & March & 0.41 & 0.05 & -0.38 & 0.35 & 0.25 & 70584 \\ 
  4 & April & 0.37 & 0.15 & -0.30 & 0.35 & 0.25 & 46666 \\ 
  5 & May & 0.27 & 0.15 & -0.45 & 0.34 & 0.24 & 72829 \\ 
  6 & June & 0.72 & -0.22 & -0.28 & 0.40 & 0.28 & 60437 \\ 
  \hline
\end{tabular}
\end{table}

As described in the xtable documentation:

digits

Numeric vector of length equal to one (in which case it will be replicated as necessary) or to the number of columns of the resulting table or matrix of thes ame size as the resulting table indicating the number of digits to display in the corresponding columns. Since the row names are printed in the first column, the length of the vector digits or the number of columns of the matrix digits is one greater than ncol(x) if x is a data.frame. Default depends of class of x.

If values of digits are negative, the corresponding values of x are displayed in scientific format with abs(digits) digits.

Upvotes: 2

Related Questions