Replace column names in kable/R markdown

前端 未结 2 2107
离开以前
离开以前 2021-02-07 12:52

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, rep

2条回答
  •  后悔当初
    2021-02-07 13:51

    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:

    HTML output:

提交回复
热议问题