1

I don't know why I'm finding this so difficult, but I'm trying increase the number of decimal places in my printed output.

The current output looks like this:

rating
 0.98
 0.99
 0.87  

I would like it to look as such:

 rating
 0.9800
 0.9900
 0.8700

I tried df %>% mutate(rating = round(rating, 4) but that just returns the same results with only two decimal places.

  • 1
    `options(digits=4)`. If you're rendering to some "fancy table" (e.g., `knitr::kable`, `formattable`, etc), then you will need to review docs specific to that package to get it to display what you want. – r2evans Mar 05 '21 at 22:14

1 Answers1

0

We could use sprintf or format to do the formatting to return a character column

library(dplyr)
df %>% 
     mutate(rating = sprintf('%.4f', rating))
#  rating
#1 0.9800
#2 0.9900
#3 0.8700

With tidyverse, the print.tbl behaves slightly differently by doing its own formatting. So, if the question is about the way it is printed, we need to change the options for printing tibble

data

df <- structure(list(rating = c(0.98, 0.99, 0.87)),
      class = "data.frame", row.names = c(NA, -3L))
akrun
  • 874,273
  • 37
  • 540
  • 662