In the R package gt, fmt_scientific()
allows you to select specific columns and specific rows to format with scientific notation. I want to apply fmt_scientific()
to many columns but only to apply to values below or above a certain threshold, let's say below 0.0001 and above 100000. Otherwise display the values in standard notation. This can be done column by column with a separate call to fmt_scientific()
for each column, specifying the threshold. But that may be cumbersome if there are many columns to format in this way. Is there a concise way to do this?
The below code gives the desired output but is there a way to apply this to many columns without repeating fmt_scientific()
many times?
library(gt)
fakedata <- data.frame(A = c(0.000001, 0.001, 1, 10000, 10000000),
B = c(1, 2, 3, 4, 1e9),
C = c(1, 1, 2e-8, 3e7, 0))
gt(fakedata) %>%
fmt_number(n_sigfig = 2) %>%
fmt_scientific(A, rows = A < 1e-4 | A > 1e5) %>%
fmt_scientific(B, rows = B < 1e-4 | B > 1e5) %>%
fmt_scientific(C, rows = C < 1e-4 | C > 1e5)
One option would be to use e.g. Reduce
to loop over the columns:
library(gt)
fakedata <- data.frame(
A = c(0.000001, 0.001, 1, 10000, 10000000),
B = c(1, 2, 3, 4, 1e9),
C = c(1, 1, 2e-8, 3e7, 0)
)
gt(fakedata) %>%
fmt_number(n_sigfig = 2) %>%
Reduce(
\(x, y) fmt_scientific(x, y, rows = y < 1e-4 | y > 1e5),
init = .,
c("A", "B", "C")
)