5

Although basic, I can't find an answer anywhere: how can I disable scientific notation in tibbles, and have the tibble display decimals instead?

My data

I have a simple tibble, resulted from lm() %>% broom::tidy().

library(tidyverse)

## I used dput() to get this:

tidy_lm_output <- structure(list(term = c("(Intercept)", "mood", "sleep"), estimate = c(-0.00000000000000028697849703988, 
-0.0746522106739049, 0.835867664974019), std.error = c(0.0319620048196539, 
0.0464197056030362, 0.0464197056030362), statistic = c(-0.00000000000000897873893265334, 
-1.60820086435494, 18.006742053085), p.value = c(0.999999999999993, 
0.108628280589954, 9.41480010964234e-53)), row.names = c(NA, 
-3L), class = c("tbl_df", "tbl", "data.frame"))

> tidy_lm_output
## # A tibble: 3 x 5
##  term         estimate std.error statistic   p.value
##   <chr>           <dbl>     <dbl>     <dbl>     <dbl>
## 1 (Intercept) -2.87e-16    0.0320 -8.98e-15 10.00e- 1
## 2 mood        -7.47e- 2    0.0464 -1.61e+ 0  1.09e- 1
## 3 sleep        8.36e- 1    0.0464  1.80e+ 1  9.41e-53

Strangely enough, only the "std.error" column displays in decimals, but all the other columns are in scientific notation.

I'd like to get all columns to show information in decimals, while still as a tibble.

Attempts to solve this

  • The go-to solution of options(scipen=999) did not work.
  • Using format(scientific = FALSE) didn't work either.

This questions asked elsewhere

  • Was asked here by somebody but remained unanswered.
Emman
  • 3,695
  • 2
  • 20
  • 44
  • Do you _really_ want 53 zeros in your p-value column? I don't think you do – Hong Ooi Jan 31 '20 at 13:15
  • Not 53 zeros, but I'll be happy with some rounding too. If the number means practically 0, then be it. I just want an interpretable display. – Emman Jan 31 '20 at 13:17

2 Answers2

8

You can control the number of significant digits printed by using options(pillar.sigfig = 5).

tidy_lm_output
# A tibble: 3 x 5
  term           estimate std.error   statistic    p.value
  <chr>             <dbl>     <dbl>       <dbl>      <dbl>
1 (Intercept) -2.8698e-16  0.031962 -8.9787e-15 1.0000e+ 0
2 mood        -7.4652e- 2  0.046420 -1.6082e+ 0 1.0863e- 1
3 sleep        8.3587e- 1  0.046420  1.8007e+ 1 9.4148e-53

For more info on how to format tibbles see help(format.tbl)

However, if you just want to round these figures you can do:

tidy_lm_output %>%
  mutate_if(is.numeric, round, 5)

# A tibble: 3 x 5
  term        estimate std.error statistic p.value
  <chr>          <dbl>     <dbl>     <dbl>   <dbl>
1 (Intercept)   0         0.0320      0      1    
2 mood         -0.0746    0.0464     -1.61   0.109
3 sleep         0.836     0.0464     18.0    0    
Ritchie Sacramento
  • 29,890
  • 4
  • 48
  • 56
  • 1
    It's the second part of your answer that I was looking for. – Emman Jan 31 '20 at 13:36
  • 4
    In case `mutate_if` becomes deprecated (it's now marked as superseded), the new preferred syntax for the `mutate_if` line is `mutate(across(where(is.numeric), round, 5))` – Dan Villarreal Nov 20 '20 at 16:05
3

format is a base function that does not support tibbles. So to use it, convert your tibble to a data frame first:

Tidyverse Pipe Style

https://style.tidyverse.org/pipes.html

my_formatted_df <- my_tibble %>% as.data.frame() %>% format(scientific=FALSE)

Nested Function Style

my_formatted_df <- format(as.data.frame(my_tibble), scientific = FALSE)
Arlo
  • 1,331
  • 2
  • 15
  • 26