flextable:table 的科学格式,它既有非常大的数字,也有非常小的数字
flextable: scientific formats for a table that has both very large and very small numbers
我想以科学格式显示大数字并且保持小数字的常规格式以减少列宽。实际数据的列数比此处显示的多。
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")
提供的以下代码对我不起作用。
A %>%
flextable::flextable() %>%
set_formatter(numbers = function(x) {
formatC(x, format = "e", digits = 2)
})
问题是您的数据中没有名为 numbers
的列。根据文档 (?set_formatter
) 你可以通过
Name-value pairs of functions, names should be existing col_key values
按名称指定列的格式。因此,要格式化您的大数字,您可以这样做:
library(flextable)
format_scientific <- function(x) {
formatC(x, format = "e", digits = 2)
}
A |>
flextable() |>
set_formatter(
f_1 = format_scientific,
f_2 = format_scientific,
f_3 = format_scientific
)
这是另一种选择:使用带有 scientific
和 digits
参数的 format()
函数:
library(flextable)
library(dplyr)
A %>%
mutate(across(everything(), ~
case_when(. < 10000 ~ format(., scientific = FALSE),
. >= 10000 ~ format(., scientific = TRUE, digits = 2)))) %>%
flextable()
我想以科学格式显示大数字并且保持小数字的常规格式以减少列宽。实际数据的列数比此处显示的多。
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")
A %>%
flextable::flextable() %>%
set_formatter(numbers = function(x) {
formatC(x, format = "e", digits = 2)
})
问题是您的数据中没有名为 numbers
的列。根据文档 (?set_formatter
) 你可以通过
Name-value pairs of functions, names should be existing col_key values
按名称指定列的格式。因此,要格式化您的大数字,您可以这样做:
library(flextable)
format_scientific <- function(x) {
formatC(x, format = "e", digits = 2)
}
A |>
flextable() |>
set_formatter(
f_1 = format_scientific,
f_2 = format_scientific,
f_3 = format_scientific
)
这是另一种选择:使用带有 scientific
和 digits
参数的 format()
函数:
library(flextable)
library(dplyr)
A %>%
mutate(across(everything(), ~
case_when(. < 10000 ~ format(., scientific = FALSE),
. >= 10000 ~ format(., scientific = TRUE, digits = 2)))) %>%
flextable()