Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace column names in kable/R markdown

My data frame has ugly column names, but when displaying the table in my report, I want to their "real" names including special characters '(', new lines, greek letters, repeated names, etc.

Is there an easy way of replacing the names in knitr to allow such formatting?

Proposed solution

What I have tried to do is suppress the printing of the data frame names and use add_header_above for better names and names that span several columns. Some advice I've seen says to use:

x <- kable(df)
gsub("<thead>.*</thead>", "", x) 

to remove the column names. That's fine, but the issue is that when I subsequently add_header_above, the original column names come back. If I use col.names=rep('',times=ncol(d.df)) in kable(...) the names are gone but the row remains, leaving a gap between my new column names and the table body. Here's a code chunk to illustrate:

```{r functions,echo=T}
drawTable <- function(d.df,caption='Given',hdr.above){
require(knitr)
require(kableExtra)
require(dplyr)

hdr.2 <- rep(c('Value','Rank'),times=ncol(d.df)/2)
x <- knitr::kable(d.df,format='latex',align='c',
  col.names=rep('',times=ncol(d.df))) %>%     
kable_styling(bootstrap_options=c('striped','hover',
  'condensed','responsive'),position='center',
   font_size = 9,full_width=F)

x %>% add_header_above(hdr.2) %>%
  add_header_above(hdr.above)
}
```

```{r}
df <- data.frame(A=c(1,2),B=c(4,2),C=c(3,4),D=c(8,7))
hdr.above <- c('A2','B2','C2','D2')
drawTable(df,hdr.above = hdr.above)
```

enter image description here

like image 647
Dr Dave Avatar asked Jul 19 '18 22:07

Dr Dave


People also ask

How do I rename a column in R?

Rename Column using colnames() colnames() is the method available in R base which is used to rename columns/variables present in the data frame. By using this you can rename a column by index and name. Alternatively, you can also use name() method.

How do I remove a column from a Kable in R?

You can also use kableExtra::remove_column() , to remove a selected column from your final kable table.

What is Kable () in R?

kable() is a method in R designed to generate a table against the given input. It is a part of the knitr package, which should be installed in the R environment for the kable method to run.

What R package has Kable?

The kableExtra package (Zhu 2021) is designed to extend the basic functionality of tables produced using knitr::kable() (see Section 10.1).


1 Answers

I am not sure where you got the advice to replace rownames, but it seems excessively complex. It is much easier just to use the built-in col.names argument within kable. This solution works for both HTML and LaTeX outputs:

---
output:
  pdf_document: default
  html_document: default
---
```{r functions,echo=T}
require(knitr)

df <- data.frame(A=c(1,2),B=c(4,2),C=c(3,4),D=c(8,7))
knitr::kable(df, 
             col.names = c("Space in name",
                           "(Special Characters)",
                           "$\\delta{m}_1$",
                           "Space in name"))

```

PDF output: enter image description here

HTML output: enter image description here

like image 147
Michael Harper Avatar answered Sep 22 '22 04:09

Michael Harper