to_fixed_digits.Rd
This function formats specified numeric columns in a data frame to a fixed number of decimal places.
to_fixed_digits(data, ..., .digits = 3)
A data frame or tibble containing the columns to format.
<tidy-select
> Columns to apply the fixed digit formatting to.
If no columns are specified, all numeric columns are selected.
An integer specifying the number of decimal places to format to. Default is 3.
A data frame with the selected numeric columns formatted to the specified number of decimal places.
Tibble data frames display numeric values to a certain number of significant
figures, determined by the pillar.sigfig
option. Sometimes it
may be useful or necessary to see values to a fixed number of digits. This
can be accomplished with num. This function is a convenience function that applies
num to all, or a specified subset, of the numeric vectors in a
tibble.
# Format all numeric columns to 3 decimal places
mtcars_df <- tibble::as_tibble(mtcars)
to_fixed_digits(mtcars_df)
#> # A tibble: 32 × 11
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> <num:.3!> <num:.3> <num:.> <num:.> <num> <num> <num:> <num> <num> <num> <num>
#> 1 21.000 6.000 160.000 110.000 3.900 2.620 16.460 0.000 1.000 4.000 4.000
#> 2 21.000 6.000 160.000 110.000 3.900 2.875 17.020 0.000 1.000 4.000 4.000
#> 3 22.800 4.000 108.000 93.000 3.850 2.320 18.610 1.000 1.000 4.000 1.000
#> 4 21.400 6.000 258.000 110.000 3.080 3.215 19.440 1.000 0.000 3.000 1.000
#> 5 18.700 8.000 360.000 175.000 3.150 3.440 17.020 0.000 0.000 3.000 2.000
#> 6 18.100 6.000 225.000 105.000 2.760 3.460 20.220 1.000 0.000 3.000 1.000
#> 7 14.300 8.000 360.000 245.000 3.210 3.570 15.840 0.000 0.000 3.000 4.000
#> 8 24.400 4.000 146.700 62.000 3.690 3.190 20.000 1.000 0.000 4.000 2.000
#> 9 22.800 4.000 140.800 95.000 3.920 3.150 22.900 1.000 0.000 4.000 2.000
#> 10 19.200 6.000 167.600 123.000 3.920 3.440 18.300 1.000 0.000 4.000 4.000
#> # ℹ 22 more rows
# Format columns mpg to qsec to 3 decimal places
to_fixed_digits(mtcars_df, mpg:qsec)
#> # A tibble: 32 × 11
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> <num:.3!> <num:.3> <num:.> <num:.> <num> <num> <num:> <dbl> <dbl> <dbl> <dbl>
#> 1 21.000 6.000 160.000 110.000 3.900 2.620 16.460 0 1 4 4
#> 2 21.000 6.000 160.000 110.000 3.900 2.875 17.020 0 1 4 4
#> 3 22.800 4.000 108.000 93.000 3.850 2.320 18.610 1 1 4 1
#> 4 21.400 6.000 258.000 110.000 3.080 3.215 19.440 1 0 3 1
#> 5 18.700 8.000 360.000 175.000 3.150 3.440 17.020 0 0 3 2
#> 6 18.100 6.000 225.000 105.000 2.760 3.460 20.220 1 0 3 1
#> 7 14.300 8.000 360.000 245.000 3.210 3.570 15.840 0 0 3 4
#> 8 24.400 4.000 146.700 62.000 3.690 3.190 20.000 1 0 4 2
#> 9 22.800 4.000 140.800 95.000 3.920 3.150 22.900 1 0 4 2
#> 10 19.200 6.000 167.600 123.000 3.920 3.440 18.300 1 0 4 4
#> # ℹ 22 more rows
# Format specific columns to 2 decimal places
to_fixed_digits(mtcars_df, mpg, hp, .digits = 2)
#> # A tibble: 32 × 11
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> <num:.2!> <dbl> <dbl> <num:.2!> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21.00 6 160 110.00 3.9 2.62 16.5 0 1 4 4
#> 2 21.00 6 160 110.00 3.9 2.88 17.0 0 1 4 4
#> 3 22.80 4 108 93.00 3.85 2.32 18.6 1 1 4 1
#> 4 21.40 6 258 110.00 3.08 3.22 19.4 1 0 3 1
#> 5 18.70 8 360 175.00 3.15 3.44 17.0 0 0 3 2
#> 6 18.10 6 225 105.00 2.76 3.46 20.2 1 0 3 1
#> 7 14.30 8 360 245.00 3.21 3.57 15.8 0 0 3 4
#> 8 24.40 4 147. 62.00 3.69 3.19 20 1 0 4 2
#> 9 22.80 4 141. 95.00 3.92 3.15 22.9 1 0 4 2
#> 10 19.20 6 168. 123.00 3.92 3.44 18.3 1 0 4 4
#> # ℹ 22 more rows