用列的单位得到第二个 header

Get a second header with the units of columns

有时在学术文本中,人们想要呈现一个 Table,其中每一列都有单位。通常在列名下面指定单位,像这样

|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 文档实现这一点?

提前致谢。

下面是使用 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")
```