Search code examples
rrstudioknitrdplyrxtable

dplyr adding a label to a group of columns in a table


When creating an R markdown report in Rstudio, I would like to make my tables a little easier to understand. I have looked into kable() and xtable(), but I have not found what I'm looking for (or perhaps haven't understood what I've found). Here is a sample table that I might include:

library(dplyr)
library(tidyr)
library(knitr)

mtcars %>% 
 group_by(gear,cyl) %>%
 summarize(count = n()) %>%
 spread(cyl,count) %>%
 kable()

Here is the (console) result:

| gear|  4|  6|  8|
|----:|--:|--:|--:|
|    3|  1|  2| 12|
|    4|  8|  4| NA|
|    5|  2|  1|  2|

In a report, I'd like to include the column name "Cyl" (or even better "Cylinder") above the 4/6/8. Otherwise, in complex tables, it may not be clear what those values represent.

Specifically: How can I add a row to the start of this table that displays "Cylinder" above the final three columns?

Thanks for the help!


Solution

  • It's not specifically what I was looking for, but I settled for just changing each individual column name. It's basic, but for anyone looking here in the future, my code now looks like this:

    library(dplyr)
    library(tidyr)
    library(knitr)
    
    test <- mtcars %>% 
     group_by(gear,cyl) %>% 
     summarize(count = n()) %>%
     spread(cyl,count)
    
    colnames(test)[2:4] <- paste(c(4,6,8),"Cylinder",sep=" ")
    
    test %>% kable()
    

    The resulting table looks like this in the console:

    | gear| 4 Cylinder| 6 Cylinder| 8 Cylinder|
    |----:|----------:|----------:|----------:|
    |    3|          1|          2|         12|
    |    4|          8|          4|         NA|
    |    5|          2|          1|          2|