Why do tibbles and data.frames display decimal places a bit differently?

Hello. This may be a bit of a minor question. I was fiddling around with trying to create a data frame from scratch and I noticed that tibbles and data frames appear to display decimal places a bit differently. Is this a deliberate feature of tibbles, or am I doing something wrong?

An example with some fake data (hopefully reproducible):

test_tbl <- tibble(
  aa = c(5.5, 10.1, 100.3),
  bb = c(3.334, 55.123, 123.556)
)

test_df <- data.frame(
  aa = c(5.5, 10.1, 100.3),
  bb = c(3.334, 55.123, 123.556)
)

Here is how they look on the console. Note that row #3 of the bb variable is displayed as 124 in the tibble, but 123.556 in the data frame.

> test_tbl
# A tibble: 3 x 2
      aa     bb
   <dbl>  <dbl>
1   5.50   3.33
2  10.1   55.1 
3 100    124   
> test_df
     aa      bb
1   5.5   3.334
2  10.1  55.123
3 100.3 123.556

Also, when I apply the round function, it doesn't seem to do anything to the tibble display.

> round(test_df, 2)
     aa     bb
1   5.5   3.33
2  10.1  55.12
3 100.3 123.56
# notice that the bb variable went from 3 decimal places to 2 decimal places
> round(test_tbl, 2)
# A tibble: 3 x 2
      aa     bb
   <dbl>  <dbl>
1   5.50   3.33
2  10.1   55.1 
3 100    124 
# No change in decimal places

Is there a way to change the display of decimal places in tibbles? I think I found a way to change significant digits, but I wasn't able to find a way to change the decimal places display.

Thanks for reading my question!

4 Likes

You are seeing the way the generic print function prints tribbles. If you want a tibble printed the same way as a data.frame use the print.data.frame() function instead of print(). Here is an example that shows some of the differences.

suppressPackageStartupMessages(library(tidyverse))
test_tbl <- tibble(
    aa = c(5.555555555551, 0.13333, 100.3),
    bb = c(3.3344444, 554444.123, 1123.556)
)

test_df <- data.frame(
    aa = c(5.555555555551, 0.13333, 100.3),
    bb = c(3.3344444, 554444.123, 1123.556)
)

# by default the generic print function only
# prints out the highest 3 digits dark and the 
# rest grayed out (but a reprex won't show 
# that). It only prints 2 digits
# after the decimal point
print(test_tbl)
#> # A tibble: 3 x 2
#>        aa        bb
#>     <dbl>     <dbl>
#> 1   5.56       3.33
#> 2   0.133 554444   
#> 3 100       1124
# if you want to print out a tibble in the
# same way a data.frame is use print.data.frame
# instead of
print(test_df)
#>           aa           bb
#> 1   5.555556 3.334444e+00
#> 2   0.133330 5.544441e+05
#> 3 100.300000 1.123556e+03
print.data.frame(test_tbl)
#>           aa           bb
#> 1   5.555556 3.334444e+00
#> 2   0.133330 5.544441e+05
#> 3 100.300000 1.123556e+03

Created on 2018-03-01 by the reprex package (v0.2.0).

2 Likes

Dan is correct. Just wanted to add that the default printing of tibbles/options have open issues in GH, if you're curious to learn more:

3 Likes

Thank you both! This makes a lot more sense now.

2 Likes