【问题标题】:Formatting a numeric column as percent in flextable package in R在R中的flextable包中将数字列格式化为百分比
【发布时间】:2021-06-19 14:42:05
【问题描述】:

例如我使用的是虹膜数据:

library(flextable)
library(officer)
library(magrittr)

ft_test <- head(iris) %>% flextable() %>% 
  colformat_num(j = c("Sepal.Length", "Sepal.Width",
                             "Petal.Length", "Petal.Width"), digits = 1)

如果我希望使用百分比格式的“Petal.Width”值,正确的语法是什么?我找不到 colformat_percent 函数。有没有办法使用灵活的语法来弥补它?

【问题讨论】:

    标签: r flextable officer


    【解决方案1】:

    使用手册中记录的set_formatter,使用虹膜数据(!)

    
    ft_test <- head(iris) %>% flextable() %>% 
      set_formatter( Petal.Width = function(x) sprintf( "%.1f%%", x*100 ) )
    
    

    这是来自manual 的示例部分:

    
    Examples:
    
         ft <- flextable( head( iris ) )
         ft <- set_formatter( x = ft,
                 Sepal.Length = function(x) sprintf("%.02f", x),
                 Sepal.Width = function(x) sprintf("%.04f", x)
               )
         ft <- theme_vanilla( ft )
         ft
         
    
    

    【讨论】:

      猜你喜欢
      • 2011-11-01
      • 2018-06-12
      • 2016-03-13
      • 2015-07-17
      • 2018-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多