0
  library(tables)
  iris2 <- iris[ iris$Species != 'versicolor', ]
  iris2$Species <- factor(iris2$Species)
  tmp <- tabular( Petal.Width+Petal.Length + Sepal.Width+Sepal.Length ~ Species* (mean+sd),       data=iris2 )

  tmp.p <- sapply( names(iris2)[1:4], function(x) t.test( iris2[[x]] ~ iris2$Species )$p.value )
  tmp

  > tmp

                Species                        
                setosa         virginica       
                mean    sd     mean      sd    
   Petal.Width  0.246   0.1054 2.026     0.2747
   Petal.Length 1.462   0.1737 5.552     0.5519
   Sepal.Width  3.428   0.3791 2.974     0.3225
   Sepal.Length 5.006   0.3525 6.588     0.6359

I want to turn my R table into latex code. So I ran

 > latex(tmp, file = "")

% latex.default(tmp, file = "") 
%
\begin{table}[!tbp]
 \begin{center}
 \begin{tabular}{rrrrrrrrrrrrrrrr}\hline\hline
\multicolumn{1}{c}{}&\multicolumn{1}{c}{}&\multicolumn{1}{c}{}&\multicolumn{1}{c}{}&\multicolumn{1}{c}{}&\multicolumn{1}{c}{}&\multicolumn{1}{c}{}&\multicolumn{1}{c}{}&\multicolumn{1}{c}{}&\multicolumn{1}{c}{}&\multicolumn{1}{c}{}&\multicolumn{1}{c}{}&\multicolumn{1}{c}{}&\multicolumn{1}{c}{}&\multicolumn{1}{c}{}&\multicolumn{1}{c}{}\tabularnewline\hline
$0.246$&$1.462$&$3.428$&$5.006$&$0.105385589380046$&$0.173663996480184$&$0.379064369096289$&$0.352489687213451$&$2.026$&$5.552$&$2.974$&$6.588$&$0.274650055636667$&$0.551894695663983$&$0.322496638172637$&$0.635879593274432$\tabularnewline
\hline
\end{tabular}

\end{center}

\end{table}

This is what R gave me. But the problem is there is only 1 row, when there should actually be at least 5. First row is for the species names, second is petal width, third is petal length, and so on. Does this have to do with tmp being class tabular and not table? What can I change so that the table looks right in latex format? Also, is there a way of writing mean (sd) in just one column for setosa's petal width, i.e., 0.246 (0.105), instead of having two separate columns?

Adrian
  • 9,229
  • 24
  • 74
  • 132
  • I can't reproduce your problem. The output I get is: `\begin{tabular}{lcccc} \hline & \multicolumn{4}{c}{Species} \\ & \multicolumn{2}{c}{setosa} & \multicolumn{2}{c}{virginica} \\ & mean & sd & mean & \multicolumn{1}{c}{sd} \\ \hline Petal.Width & $0.246$ & $0.1054$ & $2.026$ & $0.2747$ \\ Petal.Length & $1.462$ & $0.1737$ & $5.552$ & $0.5519$ \\ Sepal.Width & $3.428$ & $0.3791$ & $2.974$ & $0.3225$ \\ Sepal.Length & $5.006$ & $0.3525$ & $6.588$ & $0.6359$ \\ \hline \end{tabular}` (with newlines that don't show properly in the comment) – Richie Cotton Jan 15 '15 at 07:28
  • What version of R/version of `tables`/OS are you using? – Richie Cotton Jan 15 '15 at 07:35
  • I have the latest version of R studio. Just installed the tables package today so I assume that's the latest version as well. And I'm using PC – Adrian Jan 15 '15 at 07:39
  • Rstudio is not R. What is your R version? See by typing `sessionInfo()`. – Roman Luštrik Jan 15 '15 at 10:33

1 Answers1

0

The output you show happens when you call

latex.default(tmp, file = "")

If you call

latex(tmp, file = "")

then you get the correct output, because it finds the appropriate method for tabular items.

Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
  • Hm, but I'm certain that I called the latter. However I am still not getting the correct output. – Adrian Jan 15 '15 at 07:51