gpt4 book ai didi

r - 在 R markdown 中使用 knitr、kableExtra 和 Latex 将 col 名称和标题(上图)设置为奇怪的字符

转载 作者:行者123 更新时间:2023-12-04 08:20:49 25 4
gpt4 key购买 nike

我正在尝试基于 diamonds 数据集在 Rmarkdown(for pdf)中创建一个表,我想更改一些列名和列名上方的标题,我想知道如何在中设置一些标题斜体或粗体,一些标题应该是一个符号(比如部分 eta 平方的那个)或一个完整的公式。我已经将我的 R 代码(并且我已经安装了 Latex)作为图片包含在表格中(尚未调整)。

```{r setup, include=FALSE}

setwd("~/Desktop/Tables")
knitr::opts_chunk$set(echo = FALSE)
# global options
options(knitr.table.format = "latex")
# show space instead of NA in tables
options(knitr.kable.NA = '')
library(tidyverse)
library(knitr)
library(kableExtra)
df = diamonds

```

```{r message=FALSE, warning=FALSE}

df_table = df %>%
summarise(avg = round(mean(price), 2),
sd = round(sd(price), 2),
n = n(),
range = round(max(price), 2)) %>%
mutate(grouping = "Total") %>%
select(grouping, avg, sd, n, range) %>%
bind_rows(df %>%
group_by(cut) %>%
summarise(avg = round(mean(price), 2),
sd = round(sd(price), 2),
n = n(),
range = round(max(price), 2)) %>%
mutate(grouping = as.character(cut)) %>%
select(grouping, avg, sd, n, range))

kable(df_table,
booktabs = TRUE,
linesep = "",
col.names = c("Grouping", "M in italic", "SD in italic", "N not
italic", "some weird formula for the range" )) %>%
kable_styling(latex_options = c("HOLD_position", "scale_down")) %>%
add_header_above(c(" " = 1, "the formula for the variance" = 4))

```

enter image description here

最佳答案

在调用 kableadd_header_above 时设置 escape = FALSE 并在列标题中使用 LaTeX。我应该承认,究竟应该添加多少反斜杠才能获得所需的结果对我来说是个谜。

kable(df_table,
booktabs = TRUE,
linesep = "",
col.names = c("Grouping", "$M$", "$SD$", "N", "$\\eta$"), escape = FALSE) %>%
kable_styling(latex_options = c("HOLD_position", "scale_down")) %>%
add_header_above(c(" " = 1, "$\\\\operatorname{Var}[X]$" = 4), escape = FALSE)

enter image description here

关于r - 在 R markdown 中使用 knitr、kableExtra 和 Latex 将 col 名称和标题(上图)设置为奇怪的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50585031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com