[R] tables: proper use of Hline() in tabular()

Liviu Andronic landronimirc at gmail.com
Thu Apr 25 18:00:11 CEST 2013


Dear all,
I am unable to understand how Hline() works in tabular(). I've read
the vignette and the help page, and here this example compiles
perfectly fine:
latex( tabular( Species + Hline() + 1
                ~ Heading()*mean*All(iris), data=iris) )

However, if I try it on my own data it fails. Consider this:
set.seed(1)
Xa <- data.frame(p=rep(c("First group","Second group","Third
group"),each=10,len=30),
                a=sample(c("Some long label","Some other long label",
"Yet another label"),
                         30, replace=TRUE),id=seq(30),
                b=round(runif(30,10,20)),
                c=round(runif(30,40,70)))

(x <- tabular(((p=factor(p))*(a=factor(a))+1) ~ (N = 1) + (b + c)*
                  (mean+sd),data=Xa))

 p            a                     N  mean  sd    mean  sd
 First group  Some long label        3 15.67 2.082 64.33  3.786
              Some other long label  4 14.75 2.630 52.25  8.461
              Yet another label      3 15.67 3.215 50.67  3.055
 Second group Some long label        2 17.00 1.414 57.50 10.607
              Some other long label  3 17.00 1.000 60.00  8.888
              Yet another label      5 15.00 3.082 58.20  8.672
 Third group  Some long label        4 13.75 3.594 58.75  5.909
              Some other long label  4 13.50 1.732 46.50  3.786
              Yet another label      2 16.00 1.414 50.00  4.243
              All                   30 15.13 2.501 55.37  8.045


I would like to place an Hline() between rows 3:4, rows 6:7, rows
9:10. But either way I place it I get something that doesn't compile
in LaTeX ("! Misplaced \noalign." error). For example,
x <- tabular(((p=factor(p)))*(a=factor(a)) +(Hline() + 1) ~ (N = 1) + (b + c)*
                  (mean+sd),data=Xa)
latex(x)

\begin{tabular}{llccccc}
\hline
& &  & \multicolumn{2}{c}{b} & \multicolumn{2}{c}{c} \\
p & a & N & mean & sd & mean & \multicolumn{1}{c}{sd} \\
\hline
First group & Some long label  & $\phantom{0}3$ & $15.67$ & $2.082$ &
$64.33$ & $\phantom{0}3.786$ \\
 & Some other long label  & $\phantom{0}4$ & $14.75$ & $2.630$ &
$52.25$ & $\phantom{0}8.461$ \\
 & Yet another label  & $\phantom{0}3$ & $15.67$ & $3.215$ & $50.67$ &
$\phantom{0}3.055$ \\
Second group & Some long label  & $\phantom{0}2$ & $17.00$ & $1.414$ &
$57.50$ & $10.607$ \\
 & Some other long label  & $\phantom{0}3$ & $17.00$ & $1.000$ &
$60.00$ & $\phantom{0}8.888$ \\
 & Yet another label  & $\phantom{0}5$ & $15.00$ & $3.082$ & $58.20$ &
$\phantom{0}8.672$ \\
Third group & Some long label  & $\phantom{0}4$ & $13.75$ & $3.594$ &
$58.75$ & $\phantom{0}5.909$ \\
 & Some other long label  & $\phantom{0}4$ & $13.50$ & $1.732$ &
$46.50$ & $\phantom{0}3.786$ \\
 & Yet another label  & $\phantom{0}2$ & $16.00$ & $1.414$ & $50.00$ &
$\phantom{0}4.243$ \\
 & \hline %  &  &  &  &  &  \\
 & All  & $30$ & $15.13$ & $2.501$ & $55.37$ & $\phantom{0}8.045$ \\
\hline
\end{tabular}


Please advise how to use Hline() in the example above. Regards,
Liviu

-- 
Do you know how to read?
http://www.alienetworks.com/srtest.cfm
http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader
Do you know how to write?
http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail



More information about the R-help mailing list