How to Make Tibbles Display Significant Digits

How to make tibbles display significant digits

you can set the option pillar.sigfig

options(pillar.sigfig = 1)
as_tibble(iris)
# # A tibble: 150 x 5
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species
# <dbl> <dbl> <dbl> <dbl> <fct>
# 1 5. 4. 1. 0.2 setosa
# 2 5. 3 1. 0.2 setosa
# 3 5. 3. 1. 0.2 setosa
# 4 5. 3. 2. 0.2 setosa
# 5 5 4. 1. 0.2 setosa
# 6 5. 4. 2. 0.4 setosa
# 7 5. 3. 1. 0.3 setosa
# 8 5 3. 2. 0.2 setosa
# 9 4. 3. 1. 0.2 setosa
# 10 5. 3. 2. 0.1 setosa


options(pillar.sigfig = 7)
tb = tibble(x=x)
tb
# # A tibble: 1 x 1
# x
# <dbl>
# 1 1234.568

See also:

?`tibble-options`

or online:

https://www.rdocumentation.org/packages/tibble/versions/1.4.2/topics/tibble-options

number printing format of tibbles

The easiest way to change the format of a printed tibble is to create a function that prints a mutated version of the tibble.

You can use a little non-standard evaluation to pass any functions you like to apply to each column. This is very close to what you wanted I think:

library(tidyverse)
library(scales)

format_tibble <- function(tbl, ...)
{
functions <- rlang::dots_list(...)
if(length(functions) > 0)
{
if(length(tbl) < length(functions)) functions <- functions[seq_along(tbl)]
columns <- names(functions)
for(i in seq_along(columns))
{
fun <- functions[[i]]
col <- as.name(columns[i])
tbl <- mutate(tbl, !!quo_name(col) := fun(!!enquo(col)))
}
}
print(tbl)
}

So now, taking your tibble:

t <- tibble( surface = c(98000, 178000000000, 254000000), 
price = c(517244, 939484, 1340612),
rate = c(0.12, 0.07, 0.045))

We only need to do this:

t %>%
format_tibble(surface = label_number_si(),
price = label_dollar(),
rate = label_percent())
#> # A tibble: 3 x 3
#> surface price rate
#> <chr> <chr> <chr>
#> 1 98K $517,244 12.0%
#> 2 178B $939,484 7.0%
#> 3 254M $1,340,612 4.5%

Created on 2020-02-25 by the reprex package (v0.3.0)

How to convert scientific notation to decimal in tibbles?

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

How to make R display 19 digit number as it is stored?

Sorry to be bearer of bad news but R only has

  • a numeric type (double) using 64 bits and approximately sixteen decimals precision
  • an integer type (int) using 32 bits

There is nothing else. You may force the print function to show you nineteen digits but that just means ... you are looking at three digits of randomness.

19 digits for (countable) items are common, and often provided by (signed or unsigned) int64_t types. Which R does not have natively but approximates via the integer64 call in the bit64 package.

So the following may be your only workaround:

> suppressMessages(library(bit64))
> x <- as.integer64("123456790123456789")
> x
integer64
[1] 123456790123456789
> x - 1
integer64
[1] 123456790123456788
>

The good news is that integer64 is reasonably well supported by data.table and a number of other packages.

PS It really is 19 digits where it bites:

> as.integer64(1.2e18) + 1
integer64
[1] 1200000000000000001
> as.integer64(1.2e19) + 1
integer64
[1] <NA>
Warning message:
In as.integer64.double(1.2e+19) : NAs produced by integer64 overflow
>


Related Topics



Leave a reply



Submit