【问题标题】:conditionally bold values in flextableflextable中的有条件粗体值
【发布时间】:2021-12-15 08:03:12
【问题描述】:

是否可以根据 tstat 粗体/着色估计值。

对于 ex - 如果 tstat 大于 1.96,则为粗体估计值

这是my previous question 的延续,我必须使用 flextable。

library(dplyr)
library(flextable)


attribute <- c("b0", "b1", "b2", "b3", "b4", "b5")
estimate <- round(runif(n = 6, min = 0, max = 5), 2)
tstat <- round(runif(n = 6, min = 0, max = 5), 2)

# tibble
tbl <- tibble(attribute, estimate, tstat) %>%
  as_flextable()

【问题讨论】:

  • A、B、C、D、E、F :-)
  • @akrun 抱歉,我已将其删除

标签: r dplyr flextable


【解决方案1】:

我们可以使用ifelsecase_whenbolding 添加**&lt;value&gt;**

library(dplyr)
library(flextable)
tbl <- tibble( attribute, estimate, tstat) %>%
       mutate(estimate = case_when(tstat > 1.96 
            ~sprintf('**%0.2f**', estimate), 
          TRUE  ~sprintf('%0.2f', estimate))) %>%
  as_flextable() %>%
  colformat_md()
tbl

-输出

【讨论】:

  • 好的,非常感谢,虽然我必须根据 tstat 将估计值加粗
  • @SiD 更简单,只需根据 tstat' 的比较将其更改为 estimate。更新
  • 是的,再次感谢您的工作
【解决方案2】:

另一种解决方案:

library(dplyr)
library(flextable)

set.seed(4123)

attribute <- c("b0", "b1", "b2", "b3", "b4", "b5")
estimate <- round(runif(n = 6, min = 0, max = 5), 2)
tstat <- round(runif(n = 6, min = 0, max = 5), 2)

tbl <- tibble(attribute, estimate, tstat)
  
tbl %>%   
  flextable() %>% 
  bold(tbl$tstat > 1.96,2)

【讨论】:

    【解决方案3】:

    另一种使用flextable的解决方案行选择器

    library(dplyr)
    library(flextable)
    
    set.seed(4123)
    
    attribute <- c("b0", "b1", "b2", "b3", "b4", "b5")
    estimate <- round(runif(n = 6, min = 0, max = 5), 2)
    tstat <- round(runif(n = 6, min = 0, max = 5), 2)
    
    tbl <- tibble(attribute, estimate, tstat)
    
    tbl %>%   
      flextable() %>% 
      bold(~ tstat > 1.96,2)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-31
      • 2011-08-21
      • 2020-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      相关资源
      最近更新 更多