获取具有列单位的第二个标题

问题描述 投票:1回答:1

有时在学术文本中,人们想要呈现一个表,其中每列都有单位。通常在列名称下面指定单位,如下所示

|Object       |Volume  |   area  | Price    | 
|             |$cm^3$  |$cm^2$   |   euros  |
|:------------|:-------|--------:|---------:|
|A            |3       |    43.36|    567.40|
|B            |15      |    43.47|   1000.80|
|C            |1       |    42.18|      8.81|
|D            |7       |    37.92|      4.72|

我怎么能为我的bookdown文件实现这个目标?

先感谢您。

r-markdown units-of-measurement bookdown kable
1个回答
1
投票

这是一种使用kableExtra的方法:

```{r}
library(kableExtra)
df <- data.frame(Object = LETTERS[1:5], 
                 Volume = round(runif(5, 1, 20)),
                 area   = rnorm(5, 40, 3),
                 Price  = rnorm(5, 700, 200))

colNames <- names(df)
dfUnits <- c("", "$cm^3$",  "$cm^2$", "€")

kable(df, col.names = dfUnits,escape = F, align = "c") %>%
  add_header_above(header = colNames, line = F, align = "c")
```

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.