Flextable format column to scientific notation

Viewed 392

I am trying to figure out the correct syntax for converting a column of a flextable object to scientific notation. Is this possible with the flextable package?

2 Answers

Our numbers before formatting

numbers = c(500, 1.4, 1999999, 0.3333, 0.00001)

After:

new_numbers <- formatC(numbers, format = "e", digits = 3)

Making a data.frame

data_flex <- data.frame(
     id = c(1:5),
     new_numbers
 )

Making a table

new_table <- flextable(data_flex)

You can use function set_formatter() where you can define any formatter you want.

library(flextable)
library(magrittr)

numbers <- c(500, 1.4, 1999999, 0.3333, 0.00001)
dataset <- data.frame(
  id = c(1:5),
  numbers = numbers)

flextable(dataset) %>% 
  set_formatter(numbers = function(x) {
    formatC(x, format = "e", digits = 3)
  })

enter image description here

Related