[R] Sweave: tables vs matrices

Robin Hankin r.hankin at noc.soton.ac.uk
Fri Sep 14 12:24:15 CEST 2007


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.

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



More information about the R-help mailing list