Home > Net >  flextable: scientific formats for a table that has both very large and very small numbers
flextable: scientific formats for a table that has both very large and very small numbers

Time:06-01

I want to display the large numbers in scientific format and keep the regular format for the small numbers to reduce column width. The real data have more columns than what is shown here.

dput(A)
structure(list(st2_1 = c(1437.21, 1019.03, 1199.3, 850.35, 67.32, 
586.62), st1_2 = c(0.12, 0.08, 855.64, 606.68, 687.06, 5987.41
), st2_2 = c(0.11, 0.08, 837.72, 593.98, 3659.78, 31893.5), `1-sum(e)` = c(1522.79, 
1075.11, 2092.52, 1513.48, 1052.3, 9066.99), sss_1 = c(0.05, 
0.03, 0.99, 0.7, 0.03, 0.29), sss_2 = c(0.02, 0.02, 0.43, 0.3, 
0.02, 0.19), f_1 = c(4099185668.8, 2766736655.89, 2093885714.17, 
1228712929.01, 5592860407.22, 560845021.29), f_2 = c(990125323.33, 
668282982.85, 14238156533.71, 8355091636.51, 10241029160.69, 
1026957549.35), f_3 = c(237420871.69, 160246712.8, 711346410.36, 
417425137.15, 6553794288.93, 657206264.75)), row.names = c(NA, 
6L), class = "data.frame")

The code below, provided at enter image description here

CodePudding user response:

Here is another option: Using format() function with scientific and digits argument:

library(flextable)
library(dplyr)

A %>% 
  mutate(across(everything(), ~
                  case_when(. < 10000 ~ format(., scientific = FALSE),
                            . >= 10000 ~ format(., scientific = TRUE, digits = 2)))) %>% 
  flextable()

enter image description here

  • Related