如何在 R Markdown 中为每个列加粗每个单元格的最小值

How can I bold each cell with minimum value for each column in R Markdown

我想为我合并为一个的 data frames 之一加粗具有最小值的单元格。我希望每列中的最小值加粗。

下面的 R 代码显示了我的最小工作示例 (MWE)。 table 由五 (5) 个从正态分布中随机生成的值组成。前三 (3) 个显示 std dev =1 时,后三 (3) 个显示 std dev =2 时。每列根据其生成的种子及其 std dev.

而不同
---
title: "Min Value to be Bolded"
output:  pdf_document
---

```{r}
set.seed(1)
df1 <- data.frame(
  seed1 = rnorm(5, mean = 0, sd = 1),
  seed2 = rnorm(5, mean = 0, sd = 1),
  seed3 = rnorm(5, mean = 0, sd = 1)
)
set.seed(1)
df2 <- data.frame(
  seed1 = rnorm(5, mean = 0, sd = 2),
  seed2 = rnorm(5, mean = 0, sd = 2),
  seed3 = rnorm(5, mean = 0, sd = 2)
)
df <- cbind(df1, df2)
df |>
  knitr::kable(format = "html", table.attr = "style='width:100%;'", digits = 2, align = 'c', caption = "5 Random Numbers from the Normal Dist at diff set and std dev") |>
  kableExtra::kable_styling(bootstrap_options = 'bordered') |>
  kableExtra::add_header_above(c('$sd = 1$' = 3, '$sd$ = 2' = 3)) |>
  row_spec(3, bold=T, hline_after = TRUE)
```

输出

代替我获取最小值粗体。我得到了第三行粗体。我可以这样获取 R 上的值:

lapply(df1, 2, FUN = min)

但我需要有关如何使用 kablekableExtral 加粗单元格的帮助。

请注意,我并不是要对整个数据框执行此操作,我只需要 df1df2.

您可以添加 library(formattable) 然后

df |> 
  lapply(\(x){ifelse(x == min(x), cell_spec(round(x,2), bold = TRUE), round(x,2))}) |>
  knitr::kable(format = "html", table.attr = "style='width:100%;'", digits = 2, align = 'c', 
                caption = "5 Random Numbers from the Normal Dist at diff set and std dev", 
                escape = FALSE) |>
  kableExtra::kable_styling(bootstrap_options = 'bordered') |>
  kableExtra::add_header_above(c('$sd = 1$' = 3, '$sd$ = 2' = 3))

或者,如果您只想要 df1,您可以从

开始
df1 |> lapply(\(x){ifelse(x == min(x), cell_spec(round(x, 2), bold = TRUE), round(x, 2))}) |> cbind(df2) |> 

df2

df1 |> cbind(df2 |> lapply(\(x){ifelse(x == min(x), cell_spec(round(x, 2), bold = TRUE), round(x, 2))})) |>

备注:在我的解决方案中,dataframe 的名称全部变为 x。您可以在 lapply 之后添加 as.data.frame 以获取原始名称,但在这种情况下,它们会发生变化,因为 df 具有重复的名称,这不是一种合适的做法。相反,使用 col.names 您可以编写所需的,例如

df |> 
  lapply(\(x){ifelse(x == min(x), cell_spec(round(x,2), bold = TRUE), round(x,2))}) |>
  as.data.frame() |>
  knitr::kable(format = "html", table.attr = "style='width:100%;'", digits = 2, align = 'c', 
               caption = "5 Random Numbers from the Normal Dist at diff set and std dev", 
               escape = FALSE, col.names = rep(c("seed1", "seed2", "seed3"), 2)) |>
  kableExtra::kable_styling(bootstrap_options = 'bordered') |>
  kableExtra::add_header_above(c('$sd = 1$' = 3, '$sd$ = 2' = 3)) 

另请注意,作为评论@phiver,我必须将 output: pdf_document 修改为 output: html_document,否则代码将无法运行。要获得 pdf,代码的最后几行应如下所示(然后有效):

  knitr::kable(format = "latex",  digits = 2, align = 'c', 
               caption = "5 Random Numbers from the Normal Dist at diff set and std dev", 
               escape = FALSE, col.names = rep(c("seed1", "seed2", "seed3"), 2)) |>
  kableExtra::kable_styling(bootstrap_options = 'bordered', full_width = TRUE) |>
  kableExtra::add_header_above(c('$sd = 1$' = 3, '$sd$ = 2' = 3))