Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set digits row wise with print.xtable in R?

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.

like image 774
Matt Bannert Avatar asked Jan 18 '13 17:01

Matt Bannert


1 Answers

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}
like image 70
Eric Fail Avatar answered Sep 25 '22 20:09

Eric Fail