-2

I have a table as such:

    +-----------+----------------+----------+  
    |           |     Type 1     | Type 2   |
    +-----------+----------------+----------+     
    |    Mean   |     31,2       |    16,0  |
    |   Median  |     51,3       |    16,0  |
    |    Max    |     40,4       |    6,0   |
    |    Min    |     100,0      |    16,0  |
    |    Q1     |     34,6       |    16,0  |
    |    Q3     |     16,0       |    16,0  |
    +---+------------+-----------+----------+ 

How can I add a title to it, so it becomes:

    +-----------+----------------+----------+  
    |        INSERT TABLE TITLE HERE        |   
    +-----------+----------------+----------+  
    |           |     Type 1     | Type 2   |
    +-----------+----------------+----------+     
    |    Mean   |     31,2       |    16,0  |
    |   Median  |     51,3       |    16,0  |
    |    Max    |     40,4       |    6,0   |
    |    Min    |     100,0      |    16,0  |
    |    Q1     |     34,6       |    16,0  |
    |    Q3     |     16,0       |    16,0  |
    +---+------------+-----------+----------+ 
Fratel
  • 57
  • 1
  • 9

2 Answers2

0

you can use tableGrob from package gridExtra
check this post : Merging Table Header Cells Using tableGrob

Community
  • 1
  • 1
s.brunel
  • 1,003
  • 10
  • 24
0

If you are talking about output tables for LaTeX, HTML or Word, then you want to use a package for that purpose. There's a list of possible packages here. (Disclaimer: I wrote huxtable, which is one of the options.) Or you could just use knitr's built in kable.

In huxtable, here's how to do it:

library(huxtable)
# sample data:
tbl <- table(sample(letters[1:5], 20, replace = TRUE), 
      sample(LETTERS[11:15], 20, replace = TRUE))
huxtbl <- as_hux(tbl, add_rownames = FALSE)
huxtbl <- rbind(rep('', 5), huxtbl)
colspan(huxtbl)[1, 1] <- 5
huxtbl[1, 1] <- 'Table title goes here'
huxtbl

Which produces something like:

  V1   V2   V3   V4   V5   
   Table title goes here    
   K    L    M    N    O    
   0    0    1    1    1    
   0    1    3    1    0    
   1    0    2    0    0    
   1    2    1    1    1    
   0    1    0    2    0 

on-screen, and exports the table to markdown, HTML or LaTeX.