[R] Sweave: tables vs matrices

Gavin Simpson gavin.simpson at ucl.ac.uk
Fri Sep 14 10:52:03 CEST 2007


On Fri, 2007-09-14 at 09:34 +0100, Robin Hankin wrote:
> 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?

See ?print.default and its argument na.print:

> print.default(jj, na.print = "-")
     [,1] [,2] [,3] [,4] [,5]
[1,]    2    3    4    1   10
[2,]    0    5    7    -   12
[3,]    3    7    -    4   14
[4,]    2    -    -    2    4
[5,]    7   15   11    7   40

Is that what you meant? It still prints the [1,] bits...

HTH

G

> 
> 
> 
> \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
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Gavin Simpson                 [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list