10

I got some report that has a few tables which contain proportion AND absolute values. Now I'd like to display rows that contain proportion at a digits=2 accuracy while absolute values are just shown with digits=0. I know this is possible out-of-the-box with xtable using a call like this

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

But is there any way of doing the same thing row wise?

EDIT: Here's a shot at a reproducible example

require(xtable)
df <- data.frame(col1=rnorm(10),col2=runif(10,1,3))
xtable(df,digits=c(0,5,0))

echos

% latex table generated in R 2.15.1 by xtable 1.7-0 package
% Fri Jan 18 18:58:47 2013
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrr}
\hline
& col1 & col2 \\ 
\hline
1 & -0.79222 & 3 \\ 
2 & 0.24845 & 2 \\ 
3 & 0.24217 & 2 \\ 
4 & -0.19935 & 2 \\ 
5 & 0.47873 & 2 \\ 
6 & 1.10494 & 2 \\ 
7 & 1.54076 & 1 \\ 
8 & 0.25272 & 2 \\ 
9 & -0.11308 & 1 \\ 
10 & -1.23875 & 2 \\ 
\hline
\end{tabular}
\end{center}
\end{table}

This is the solution for the column case. I get 2 different numeric columns, one with 5 digits and one fully rounded.

What I want is e.g.: line 2 and 10 5 digits all others 0 digits.

Matt Bannert
  • 27,631
  • 38
  • 141
  • 207
  • 1
    A reproducible example will up your chances of getting a helpful reply... – Josh O'Brien Jan 18 '13 at 17:30
  • Hard to reproduce what you can't do. But of course you are always right when you encourage to try to put up something reproducible. thx for the butt kick! – Matt Bannert Jan 18 '13 at 18:03
  • Hah! You went above and beyond. (I was just asking for an example of the R object you were wanting to print.) – Josh O'Brien Jan 18 '13 at 18:07
  • I suspect you can hack it together by creating two tables, one with 5 digits and one with two digits, and printing different parts of them with `print(xtable.5digits[1:5,],only.contents=TRUE)` and `print(xtable.0digits[6:10,],only.contents=TRUE)`. It will be ugly, though. – Jonathan Christensen Jan 18 '13 at 18:14

1 Answers1

8

My matrix-generating-skills are not the most elegant, but is this what you are looking for?

require(xtable)
df <- data.frame(col1=rnorm(10),col2=runif(10,1,3))
mdat <- matrix(c(rep(0,3), 0, 5, 0, rep(0, (7*3)), 0, 5, 0), 
                 nrow = 10, ncol=3, byrow=TRUE)
xtable(df,digits=mdat)
% latex table generated in R 2.15.1 by xtable 1.7-0 package
% Fri Jan 18 15:52:41 2013
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrr}
  \hline
 & col1 & col2 \\ 
  \hline
  1  &      -0 & 3 \\ 
  2  & 1.16203 & 1 \\ 
  3  &       1 & 2 \\ 
  4  &       0 & 2 \\ 
  5  &       0 & 2 \\ 
  6  &      -1 & 2 \\ 
  7  &       1 & 2 \\ 
  8  &       1 & 2 \\ 
  9  &      -1 & 2 \\ 
  10 & 0.63731 & 2 \\ 
   \hline
\end{tabular}
\end{center}
\end{table}

Or the whole row with,

require(xtable)
df <- data.frame(col1=rnorm(10),col2=runif(10,1,3))
mdat <- matrix(c(rep(0,3),rep(5,3), rep(0, (7*3)), rep(5,3)),
                 nrow = 10, ncol=3, byrow=TRUE)
xtable(df,digits=mdat)
% latex table generated in R 2.15.1 by xtable 1.7-0 package
% Fri Jan 18 16:00:12 2013
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrr}
  \hline
 & col1 & col2 \\ 
  \hline
  1  &       -0 & 1       \\ 
  2  &  1.36652 & 2.10159 \\ 
  3  &        0 & 2       \\ 
  4  &       -2 & 2       \\ 
  5  &       -1 & 2       \\ 
  6  &        0 & 2       \\ 
  7  &       -0 & 2       \\ 
  8  &       -0 & 1       \\ 
  9  &       -1 & 2       \\ 
  10 & -0.44182 & 2.09663 \\ 
   \hline
\end{tabular}
\end{center}
\end{table}
Eric Fail
  • 8,191
  • 8
  • 72
  • 128
  • +1. Very helpful indeed, that's exactly what I was looking for. Maybe you should sent the author of the package an hint. I mean the documentation almost discourages to try that. Anyway, thx your suggestion was of great help! – Matt Bannert Jan 19 '13 at 11:06
  • 2
    @MattBannert, I figured it out by reading the `?xtable` help file. I didn't know this before reading "**or** matrix of the same size as the resulting table indicating the number of digits to display in the corresponding columns" (`?xtable::xtable`). The **or** is bold in the help-file. Regardless, I often overlook stuff in the help-file. – Eric Fail Jan 19 '13 at 19:42
  • Thanks for the hint even more then. Sorry for the noise! Hopefully it helps some googlers. – Matt Bannert Jan 20 '13 at 17:56
  • 1
    shorter solution for the latter case (what I think OP was going for) is to set `mdat<-matrix(c(0,5,rep(0,7),5),nrow=10,ncol=3)` since it will repeat the rest by recycling. That or `mdat<-matrix(0,nrow=10,ncol=3); mdat[c(2,10),]<-5` – MichaelChirico Oct 19 '15 at 23:03