繁体   English   中英

flextable 中的有条件粗体值

[英]conditionally bold values in flextable

是否可以根据 tstat.粗体/颜色估计值?

例如,如果 tstat 大于 1.96,则为粗体估计值

这是我之前问题的延续, 必须使用 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()

我们可以使用ifelsecase_whenbold添加**<value>**

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

-输出

在此处输入图片说明

另一种解决方案:

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)

在此处输入图片说明

使用 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)

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM