[R] Problem setting the number of digits in xtable

Brian Diggs diggsb at ohsu.edu
Wed Nov 24 22:20:26 CET 2010


On 11/22/2010 9:16 AM, wphantomfr wrote:
> DEar list members,
>
> I am currently using Sweave with LaTeX which is great.
>
> I can use xtable for formatting outp of tables but I have a problem
> setting the number of decimals in xtables when used with dataframe.
>
> I have found an example on the net ith matrix and it works.
>
> For example this works :
>>> tmp<- matrix(rnorm(9), 3, 3)
>>> xtmp<- xtable(tmp)
>>> digits(xtmp)<- c(0,0,3,4)
>>> print(xtmp, include.rownames = FALSE) # row names
>
> produced :
>> % latex table generated in R 2.12.0 by xtable 1.5-6 package
>> % Mon Nov 22 17:35:00 2010
>> \begin{table}[ht]
>> \begin{center}
>> \begin{tabular}{rrr}
>>    \hline
>> 1&  2&  3 \\
>>    \hline
>> -2&  -2.158&  2.8886 \\
>>    1&  1.330&  0.4677 \\
>>    -0&  0.486&  -0.3319 \\
>>     \hline
>> \end{tabular}
>> \end{center}
>> \end{table}
>
>
>
> But this won't work :
>>> mydata
>>    	TEST                t ddl                   p CONDITION
>> 2   R1 3.01109061083632  16 0.00828552765650315        C1
>> 3   R2 3.30476953908811  16 0.00447412002109504        C1
>> 4   DR 2.86343993410509  16  0.0112631908739966        C1
>> 5   R1 1.05386387510206  16    0.30760068470456        C2
>> 6   R2 3.04997140665209  16 0.00763921045771104        C2
>> 7   DR 2.25175987512241  16  0.0387401575011488        C2
>
> but
>>> xtable(mydata,digits=2)
>
> produced
>
>> % latex table generated in R 2.12.0 by xtable 1.5-6 package
>> % Mon Nov 22 18:13:47 2010
>> \begin{table}[ht]
>> \begin{center}
>> \begin{tabular}{rlllll}
>>    \hline
>>   &  TEST&  t&  ddl&  p&  CONDITION \\
>>    \hline
>> 2&  R1&  3.01109061083632&  16&  0.00828552765650315&  C1 \\
>>    3&  R2&  3.30476953908811&  16&  0.00447412002109504&  C1 \\
>>    4&  DR&  2.86343993410509&  16&  0.0112631908739966&  C1 \\
>>    5&  R1&  1.05386387510206&  16&  0.30760068470456&  C2 \\
>>    6&  R2&  3.04997140665209&  16&  0.00763921045771104&  C2 \\
>>    7&  DR&  2.25175987512241&  16&  0.0387401575011488&  C2 \\
>>     \hline
>> \end{tabular}
>> \end{center}
>> \end{table}
>
>
>
>
> I have also tried setting the digits with c(0,0,4,0,4,0), using also
> the 'display' argument to specify the type of each column... noway...
>
>
> What am I missing ?

My guess is that the data frame is not what you think it is.  In 
particular, I bet the t and p columns are not really numbers, but 
strings or factors.  Does str(mydata) show this?

> Thanks in advance
>
> Sylvain Clément

-- 
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University



More information about the R-help mailing list