rlatexdata-manipulationxtable

set different Digits in xtable


I thave a data set like mtcars and I would like to print it using xtable with digits =2 but the column carb I want to see the values with out digits

xtable(head(mtcars), digits =2)

Expected Results, please see the last column

% latex table generated in R 4.0.2 by xtable 1.8-4 package
% Mon Oct 12 10:10:25 2020
\begin{table}[ht]
\centering
\begin{tabular}{rrrrrrrrrrrr}
  \hline
 & mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb \\ 
  \hline
Mazda RX4 & 21.00 & 6.00 & 160.00 & 110.00 & 3.90 & 2.62 & 16.46 & 0.00 & 1.00 & 4.00 & 4 \\ 
  Mazda RX4 Wag & 21.00 & 6.00 & 160.00 & 110.00 & 3.90 & 2.88 & 17.02 & 0.00 & 1.00 & 4.00 & 4\\ 
  Datsun 710 & 22.80 & 4.00 & 108.00 & 93.00 & 3.85 & 2.32 & 18.61 & 1.00 & 1.00 & 4.00 & 1 \\ 
  Hornet 4 Drive & 21.40 & 6.00 & 258.00 & 110.00 & 3.08 & 3.21 & 19.44 & 1.00 & 0.00 & 3.00 & 1\\ 
  Hornet Sportabout & 18.70 & 8.00 & 360.00 & 175.00 & 3.15 & 3.44 & 17.02 & 0.00 & 0.00 & 3.00 & 2\\ 
  Valiant & 18.10 & 6.00 & 225.00 & 105.00 & 2.76 & 3.46 & 20.22 & 1.00 & 0.00 & 3.00 & 1 \\ 
   \hline
\end{tabular}
\end{table}

many thanks in advance


Solution

  • You may define the digits for each column. Your table has 12 columns. So the first 11 columns get 2 digits and the 12th column gets 0 digits.

    xtable::xtable(head(mtcars), digits = c(rep(2, 11), 0))
    

    Result

    % latex table generated in R 3.6.3 by xtable 1.8-4 package
    % Mon Oct 12 10:25:04 2020
    \begin{table}[ht]
    \centering
    \begin{tabular}{rrrrrrrrrrrr}
      \hline
     & mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb \\ 
      \hline
    Mazda RX4 & 21.00 & 6.00 & 160.00 & 110.00 & 3.90 & 2.62 & 16.46 & 0.00 & 1.00 & 4.00 & 4 \\ 
      Mazda RX4 Wag & 21.00 & 6.00 & 160.00 & 110.00 & 3.90 & 2.88 & 17.02 & 0.00 & 1.00 & 4.00 & 4 \\ 
      Datsun 710 & 22.80 & 4.00 & 108.00 & 93.00 & 3.85 & 2.32 & 18.61 & 1.00 & 1.00 & 4.00 & 1 \\ 
      Hornet 4 Drive & 21.40 & 6.00 & 258.00 & 110.00 & 3.08 & 3.21 & 19.44 & 1.00 & 0.00 & 3.00 & 1 \\ 
      Hornet Sportabout & 18.70 & 8.00 & 360.00 & 175.00 & 3.15 & 3.44 & 17.02 & 0.00 & 0.00 & 3.00 & 2 \\ 
      Valiant & 18.10 & 6.00 & 225.00 & 105.00 & 2.76 & 3.46 & 20.22 & 1.00 & 0.00 & 3.00 & 1 \\ 
       \hline
    \end{tabular}
    \end{table}