Kyle Ward
Kyle Ward

Reputation: 919

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!

Upvotes: 1

Views: 2755

Answers (2)

hugovdberg
hugovdberg

Reputation: 1631

When using HTML as the output the htmlTable package provides column grouping:

library(dplyr)
library(tidyr)
library(htmlTable)

test <- mtcars %>% 
 group_by(gear,cyl) %>% 
 summarize(count = n()) %>%
 spread(cyl,count)

htmlTable(
    test[,-1], # data.frame
    cgroup = c("Cylinders"), # Column group labels
    n.cgroup = c(3), # Number of columns per group
    rnames = test[[1]], # Row labels
    rowlabel = "Gears" # Column header for row labels
)

Cylinders vs Gears

For PDF output Hmisc::latex provides a similar syntax (which I unfortunately cannot test by lack of LaTeX on the machine I'm currently working on):

library(Hmisc)

latex(
    test[,-1], # data.frame
    cgroup = c("Cylinders"), # Column group labels
    n.cgroup = c(3), # Number of columns per group
    rowname = test[[1]], # Row labels
    rowlabel = "Gears" # Column header for row labels
)

If you want MS Word output you're in bad luck as far as I can see at this moment.

Upvotes: 2

Kyle Ward
Kyle Ward

Reputation: 919

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|

Upvotes: 0

Related Questions