[R] Sweave: tables vs matrices
Gavin Simpson
gavin.simpson at ucl.ac.uk
Fri Sep 14 12:45:52 CEST 2007
On Fri, 2007-09-14 at 11:24 +0100, Robin Hankin wrote:
> Hi Gavin
>
> thanks for that. . . it does 99% of what I wanted.
> I'd forgotten about the na.print argument.
>
> It's considerably nicer than my other solution
> which converted to character, then jj[is.na(jj)] <- "-"
> then noquote(jj).
>
> But sometimes I just need nice LaTeX tables
> and I can't think of a way to arrange things
> so that: (i) I have only one set of numbers to maintain,
> and (ii) an NA appears as a "-" in the LaTeX table.
Ok, then the xtable package and function is your answer. You can use
this within Sweave but I think you need to set the output to latex in
the Sweave chunk?
Is this closer to what you want?
> print.xtable(xtable(jj), NA.string = "-")
% latex table generated in R 2.5.1 by xtable 1.4-6 package
% Fri Sep 14 11:43:34 2007
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrrrr}
\hline
& 1 & 2 & 3 & 4 & 5 \\
\hline
1 & 2.00 & 3.00 & 4.00 & 1.00 & 10.00 \\
2 & 0.00 & 5.00 & 7.00 & $-$ & 12.00 \\
3 & 3.00 & 7.00 & $-$ & 4.00 & 14.00 \\
4 & 2.00 & $-$ & $-$ & 2.00 & 4.00 \\
5 & 7.00 & 15.00 & 11.00 & 7.00 & 40.00 \\
\hline
\end{tabular}
\end{center}
\end{table}
HTH
G
>
> best wishes
>
> rksh
>
> On 14 Sep 2007, at 09:52, Gavin Simpson wrote:
>
> > 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
>
--
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
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