Reputation: 2464
I have a csv file weight.csv
with the following contents.
weight,weight_selfreport
81.5,81.66969147005445
72.6,72.59528130671505
92.9,93.01270417422867
79.4,79.4010889292196
94.6,96.64246823956442
80.2,79.4010889292196
116.2,113.43012704174228
95.4,95.73502722323049
99.5,99.8185117967332
If I do
library(readr)
Df <- read_csv('weight.csv')
Df
I get
# A tibble: 9 x 2
weight weight_selfreport
<dbl> <dbl>
1 81.5 81.7
2 72.6 72.6
3 92.9 93.0
4 79.4 79.4
5 94.6 96.6
6 80.2 79.4
7 116. 113.
8 95.4 95.7
9 99.5 99.8
If I convert that tibble to a normal data frame, I'll see more digits.
as.data.frame(Df)
weight weight_selfreport
1 81.5 81.66969
2 72.6 72.59528
3 92.9 93.01270
4 79.4 79.40109
5 94.6 96.64247
6 80.2 79.40109
7 116.2 113.43013
8 95.4 95.73503
9 99.5 99.81851
Initially I thought that if I wanted to get this type of display for the tibble, I thought I would do options(pillar.sigfig = 5)
.
However, that's not what it does.
options(pillar.sigfig = 5)
Df
# A tibble: 9 x 2
weight weight_selfreport
<dbl> <dbl>
1 81.5 81.670
2 72.600 72.595
3 92.9 93.013
4 79.4 79.401
5 94.6 96.642
6 80.2 79.401
7 116.2 113.43
8 95.4 95.735
9 99.5 99.819
And so I see that pillar.sigfig
is about controlling significant digits not decimals places.
Fine but
Upvotes: 17
Views: 7908
Reputation: 119
This might come a little late...3 years late, but it might help others looking for answers.
The issue lies with tibble
. It has a very opinionated way of representing dfs. I presume, you often do not feel the need to look at your data in this way, but if you do, there are two options I frequently use that potentially are just another workaround.
Option 1: Use num()
This neat function enforces decimals. So you can mutate()
all columns you want to format with the following:
library(tidyverse)
data <- tribble(
~ weight, ~ weight_selfreport,
81.5,81.66969147005445,
72.6,72.59528130671505,
92.9,93.01270417422867,
79.4,79.4010889292196,
94.6,96.64246823956442,
80.2,79.4010889292196,
116.2,113.43012704174228,
95.4,95.73502722323049,
99.5,99.8185117967332
)
data <-
data %>%
mutate(across(where(is.numeric), ~ num(., digits = 3)))
data
#> # A tibble: 9 × 2
#> weight weight_selfreport
#> <num:.3!> <num:.3!>
#> 1 81.500 81.670
#> 2 72.600 72.595
#> 3 92.900 93.013
#> 4 79.400 79.401
#> 5 94.600 96.642
#> 6 80.200 79.401
#> 7 116.200 113.430
#> 8 95.400 95.735
#> 9 99.500 99.819
Option 2: Use table packages
Usually, when I inspect a tibble
it is because it contains results I want to report. Thus, I use one of the many table-generator packages, e.g.
flextable
,gt
,formattable
,reactable
,Here is an example you can try using flextable
:
library(tidyverse)
data <- tribble(
~ weight, ~ weight_selfreport,
81.5,81.66969147005445,
72.6,72.59528130671505,
92.9,93.01270417422867,
79.4,79.4010889292196,
94.6,96.64246823956442,
80.2,79.4010889292196,
116.2,113.43012704174228,
95.4,95.73502722323049,
99.5,99.8185117967332
)
flextable::flextable(data)
I assume Option 1 might have been what you were looking for.
Upvotes: 6
Reputation: 31
I have the same issue. Using pillar.sigfig helps. You can also use it with round() and you have more control. But if the last figure is 0 it will not display it.
The "trick" I used was to save the results in a variable and then use print.data.frame(). Then it works fine. But maybe there is an easier solution...
Upvotes: 1