Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R package xtable, how to create a latextable with multiple rows and columns from R

Tags:

r

latex

My goal is to create latextable with multirow/multicolumn features from R. The latextable I want should look like this:

               colLabel  |  colLabel2
                         |
                 a1  a2  |   a3    a4  
 -------------------------------------
             b1  1    2  |   5     6
 rowLabel1               |
             b2  3    4  |   7     8
 --------------------------------------
             b3  9   10  |   13    14
 rowLabel2               |
             b4  11  12  |   15    16
 --------------------------------------

I am currently using xtable but I cannot figure out how to create multirow using this package. Could anyone tell me how to create such table?

Thank you in advance

like image 633
FairyOnIce Avatar asked Feb 23 '13 03:02

FairyOnIce


People also ask

What package is Xtable in R?

Generate a nicely formatted table for LaTeX or a basic table for HTML. x – An R object that could conceivably be formatted into a table. A wide range of objects are accepted.

How do you do multicolumn in LaTeX?

LaTeX Multiple Columns Text with two or double columns can be created by passing the parameter \twocolumn to the document class statement. If you want to create a document with more than two columns, use the package multicol, which has a set of commands for the same.


1 Answers

I just would like to point at a combination of Christoph's block entry (here) and Gabor's answer in the R mail list (here) working with the xtable package. Furthermore, this solution is capable of merging cells in the \multirow manner.

Here comes a MWE:

require(xtable)

# set up data frame
df <- data.frame(c(replicate(2, c("L1")), replicate(2, c("L2"))),
                 replicate(4, "b"),
                 replicate(4, runif(4, 1, 10)) )

# only needed if first column consists of numbers
df[[1]] <- as.character(df[[1]])

rle.lengths <- rle(df[[1]])$lengths
first <- !duplicated(df[[1]])
df[[1]][!first] <- ""

# define appearance of \multirow
df[[1]][first] <-
   paste0("\\midrule\\multirow{", rle.lengths, "}{*}{\\textbf{", df[[1]][first], "}}")

strCaption <- paste0("\\textbf{Table Whatever} This table is just produced with some ",
                     "random data and does not mean anything. Just to show you how ",
                     "things work.")

# set up xtable output
print(xtable(df, digits = c(0, 0, 0, 3, 1, 0, 6), # first zero "represents" row numbers which we skip later
             align = "lllrr|rr",  # align and put a vertical line (first "l" again represents column of row numbers)
             caption = strCaption, label = "testTable"),
      size = "footnotesize", #Change size; useful for bigger tables "normalsize" "footnotesize"
      include.rownames = FALSE, #Don't print rownames
      include.colnames = FALSE, #We create them ourselves
      caption.placement = "top", #"top", NULL
      hline.after=NULL, #We don't need hline; we use booktabs
      floating=TRUE, # whether \begin{Table} should be created (TRUE) or not (FALSE)
      sanitize.text.function = force, # Important to treat content of first column as latex function
      add.to.row = list(pos = list(-1,
                                   2,
                                   nrow(df)),
                        command = c(paste("\\toprule \n",  # NEW row
                                          "\\multicolumn{2}{c}{} & \\multicolumn{2}{c}{\\textbf{colLabel1}} & \\multicolumn{2}{c}{colLabel2} \\\\\n",
                                          "\\cmidrule(l){3-4} \\cmidrule(l){5-6}\n",
                                          " & & a1 & a2 & a3 & a4 \\\\\n", # NEW row 
                                          "\\midrule \n"
                                          ),
                                    paste("\\cmidrule(l){3-4} \\cmidrule(l){5-6}\n" # we may also use 'pos' and 'command' to add a midrule
                                          ),
                                    paste("\\bottomrule \n"  # paste is used as it is more flexible regarding adding lines
                                          )
                                    )
                        )
      )

Which knits the following table in LaTeX:

enter image description here

like image 78
ToJo Avatar answered Sep 19 '22 09:09

ToJo