1

I'm new to R Markdown and I'm trying to make a table which looks like this:

https://ibb.co/ggYtK6 (image)

The format of output file has to be PDF.

The goal is that I select a row from the big data frame and then put different variables of this row to table with the desired look. The most difficult in that table for me is to create a row with text which spans over all columns. How can I do this?

I can imagine how to do this with HTML tags (with colspan attribute) but I guess that it will not work with PDF...

Grace Mahoney
  • 485
  • 1
  • 7
  • 14
Piotr Orczyk
  • 13
  • 1
  • 3
  • Check out https://stackoverflow.com/questions/15488350/programmatically-creating-markdown-tables-in-r-with-knitr?rq=1 – dshkol Jan 12 '18 at 20:58

1 Answers1

2

Check out Hao Zhu's package kableExtra, which extends the functionality of the popular knitr package (referenced in the comment from @dshkol above) and allows for really easy grouping and labeling of rows.

The GitHub repository has a lot of great demos on how to group and subset tables in both HTML and LaTeX (which can then be output to PDF). For what you're trying to do, I would check out the LaTeX tutorial, under the section "Grouped Columns / Rows".

Example (borrowed from the tutorial):

library(knitr) # loads the kable package required for kableExtra
library(kableExtra)

dt <- mtcars[1:10, 1:6]
kable(dt, format = "latex", caption = "Group Rows", booktabs = T) %>%
    kable_styling() %>%
    group_rows("Group 1", 4, 7) %>%
    group_rows("Group 2", 8, 10)

table output

Grace Mahoney
  • 485
  • 1
  • 7
  • 14