[R] Sweave: tables vs matrices

Robin Hankin r.hankin at noc.soton.ac.uk
Fri Sep 14 10:34:18 CEST 2007


Hello everyone


I am preparing a document using Sweave in which I want my matrices
to appear as tables.  I am running into problems because as my
Rnw files stand, I have to  change table entries twice, once for
the matrix and once for the typeset table.

I have lots of material like the following.  How can I arrange
my Rnw file so that  I only have to change one set of figures
when my numbers change?

One reason I prefer tables here is that the NA entries
appear as "-" in the table, but as "NA" in the Schunk.
Is there a way to make the Schunk  typeset NAs
as minuses?



\begin{table}
\centering
\begin{tabular}{|cccc|c|}\hline
\multicolumn{4}{|c|}{brand}&\\ \hline
A&B&C&D&total\\ \hline
2       & 3      &  4       & 1    & 10   \\
0       & 5       & 7       & -    & 12   \\
3       & 7       & -       & 4    & 14   \\
2       & -       & -       & 2    &  4    \\ \hline
7&15&11&7&40\\ \hline
\end{tabular}
\caption{snipped caption}
\end{table}


<<>>=
jj <- matrix(c(2,  3,  4, 1,
                0,  5,  7, NA,
                3,  7, NA, 4,
                2, NA, NA, 2
                ),byrow=TRUE,nrow=4)
jj <- rbind(jj,apply(jj,2,sum,na.rm=TRUE))
jj <- cbind(jj,apply(jj,1,sum,na.rm=TRUE))
jj
@








--
Robin Hankin
Uncertainty Analyst
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
  tel  023-8059-7743



More information about the R-help mailing list