Search code examples
rknitrxtable

R, knitr, xtable, using both add.to.row and hline.after


I’m trying to produce a table with xtable in R using knitr. I have inserted a header and extra \hlines. I’m using the add.to.row command to insert my header at the top, as well as midway through the table. I can’t seem to get the hline.after command correct.

I would like my table to look like two tables (though it's one), with an hline after the 4th row of numbers and before the second heading. After using the add.to.row command, I can’t seem to put lines there.

Any suggestions?

\documentclass{article}
\usepackage[margin=.75in]{geometry}

\begin{document}

<<table, results='asis'>>=
require(knitr)
require(xtable)    

# Generate some data
set.seed(1683)
dat <- rnorm(48)
tab <- as.data.frame(matrix(dat, nrow=6, ncol=8))

addtorow <- list()
addtorow$pos <- list()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 4
addtorow$pos[[4]] <- 4
addtorow$command <- c(' & & \\multicolumn{2}{c}{Group 1} & \\multicolumn{2}{c}{Group 2} & 
                     \\multicolumn{2}{c}{Group 3}  \\\\\n',  
                   " name & $\\hat{\\beta}$ & A & B & A & B & A & B  \\\\\n", 
                   " & & & & & & & \\\\\n",
                   " name & $\\hat{\\sigma}$ & A & B & A & B & A & B  \\\\\n")


print(xtable(tab, align="rrccccccc"), add.to.row=addtorow, ,     include.rownames=FALSE, 
  include.colnames=FALSE, hline.after = c(-1, 0, 4, nrow(tab)))
@


\end{document}

Solution

  • Try adding the lines inside the add.to.row

    addtorow <- list()
    addtorow$pos <- list()
    addtorow$pos[[1]] <- 0
    addtorow$pos[[2]] <- 0
    addtorow$pos[[3]] <- 4
    addtorow$pos[[4]] <- 4
    addtorow$pos[[5]] <- 4
    addtorow$pos[[6]] <- 4
    addtorow$command <- c(' & & \\multicolumn{2}{c}{Group 1} & \\multicolumn{2}{c}{Group 2} & 
                         \\multicolumn{2}{c}{Group 3}  \\\\\n',  
                       " name & $\\hat{\\beta}$ & A & B & A & B & A & B  \\\\\n", 
                       " \\hline\n",
                       " & & & & & & & \\\\\n",
                       " \\hline\n",
                       " name & $\\hat{\\sigma}$ & A & B & A & B & A & B  \\\\\n")
    
    \begin{table}[ht]
    \centering
    \begin{tabular}{rccccccc}
      \hline
       & & \multicolumn{2}{c}{Group 1} & \multicolumn{2}{c}{Group 2} &
    \multicolumn{2}{c}{Group 3}  \\
      name & $\hat{\beta}$ & A & B & A & B & A & B  \\
      \hline
       1.28 & -0.71 & 1.30 & -1.08 & -0.32 & -0.84 & -0.75 & 0.67 \\ 
      -0.83 & -0.41 & 0.09 & 0.29 & -0.91 & -0.04 & -1.61 & -0.71 \\ 
      -1.82 & 0.16 & -0.34 & 0.39 & -1.58 & 0.44 & -0.81 & -0.89 \\ 
      -2.03 & 0.30 & 1.27 & -0.46 & -1.02 & 2.98 & -0.34 & -0.75 \\ 
      \hline
      & & & & & & & \\
      \hline
      name & $\hat{\sigma}$ & A & B & A & B & A & B  \\
     \hline
    0.88 & 1.44 & 0.11 & -0.66 & -2.31 & -1.21 & -0.06 & 0.22 \\ 
      0.43 & 1.07 & -1.10 & 0.93 & -0.72 & 1.52 & -1.52 & -0.83 \\ 
       \hline
    \end{tabular}
    \end{table}