Regular round often looses trailing 0:s as these are truncated, this function converts everything to strings with all 0:s intact so that tables have the correct representation, e.g. txtRound(1.01, digits = 1) turns into 1.0.

txtRound(x, ...)

# Default S3 method
txtRound(
  x,
  digits = 0,
  digits.nonzero = NA,
  txt.NA = "",
  dec = getOption("htmlTable.decimal_marker", default = "."),
  scientific = NULL,
  txtInt_args = getOption("htmlTable.round_int", default = NULL),
  ...
)

# S3 method for class 'table'
txtRound(x, ...)

# S3 method for class 'matrix'
txtRound(x, digits = 0, excl.cols = NULL, excl.rows = NULL, ...)

# S3 method for class 'data.frame'
txtRound(x, ..., digits = 0L)

Arguments

x

The value/vector/data.frame/matrix to be rounded

...

Passed to next method

digits

The number of digits to round each element to. For matrix or data.frame input you can provide a vector/list. An unnamed vector/list must equal the length of the columns to round. If you provide a named vector you can provide specify per column the number of digits, and then use .default for those columns that we don't need to have separate values for.

digits.nonzero

The number of digits to keep if the result is close to zero. Sometimes we have an entire table with large numbers only to have a few but interesting observation that are really interesting

txt.NA

The string to exchange NA with

dec

The decimal marker. If the text is in non-English decimal and string formatted you need to change this to the appropriate decimal indicator. The option for this is htmlTable.decimal_marker.

scientific

If the value should be in scientific format.

txtInt_args

A list of arguments to pass to txtInt() if that is to be used for large values that may require a thousands separator. The option for this is htmlTable.round_int. If TRUE it will activate the txtInt functionality.

excl.cols

Columns to exclude from the rounding procedure when provided a matrix. This can be either a number or regular expression. Skipped if x is a vector.

excl.rows

Rows to exclude from the rounding procedure when provided a matrix. This can be either a number or regular expression.

Value

matrix/data.frame

Tidy-select with data.frame

The txtRound can use data.frame for input. This allows us to use tidyselect patterns as popularized by dplyr.

See also

Other text formatters: txtInt(), txtMergeLines(), txtPval()

Examples

# Basic usage
txtRound(1.023, digits = 1)
#> [1] "1.0"
# > "1.0"

txtRound(pi, digits = 2)
#> [1] "3.14"
# > "3.14"

txtRound(12344, digits = 1, txtInt_args = TRUE)
#> [1] "12,344.0"
# > "12,344.0"


# Using matrix
mx <- matrix(c(1, 1.11, 1.25,
               2.50, 2.55, 2.45,
               3.2313, 3, pi),
             ncol = 3, byrow=TRUE)
txtRound(mx, digits = 1)
#>      [,1]  [,2]  [,3] 
#> [1,] "1.0" "1.1" "1.2"
#> [2,] "2.5" "2.5" "2.5"
#> [3,] "3.2" "3.0" "3.1"
#> [,1]  [,2]  [,3]
#> [1,] "1.0" "1.1" "1.2"
#> [2,] "2.5" "2.5" "2.5"
#> [3,] "3.2" "3.0" "3.1"

# Using a data.frame directly
library(magrittr)
data("mtcars")
# If we want to round all the numerical values
mtcars %>%
  txtRound(digits = 1)
#>                      mpg cyl  disp    hp drat  wt qsec  vs  am gear carb
#> Mazda RX4           21.0 6.0 160.0 110.0  3.9 2.6 16.5 0.0 1.0  4.0  4.0
#> Mazda RX4 Wag       21.0 6.0 160.0 110.0  3.9 2.9 17.0 0.0 1.0  4.0  4.0
#> Datsun 710          22.8 4.0 108.0  93.0  3.9 2.3 18.6 1.0 1.0  4.0  1.0
#> Hornet 4 Drive      21.4 6.0 258.0 110.0  3.1 3.2 19.4 1.0 0.0  3.0  1.0
#> Hornet Sportabout   18.7 8.0 360.0 175.0  3.1 3.4 17.0 0.0 0.0  3.0  2.0
#> Valiant             18.1 6.0 225.0 105.0  2.8 3.5 20.2 1.0 0.0  3.0  1.0
#> Duster 360          14.3 8.0 360.0 245.0  3.2 3.6 15.8 0.0 0.0  3.0  4.0
#> Merc 240D           24.4 4.0 146.7  62.0  3.7 3.2 20.0 1.0 0.0  4.0  2.0
#> Merc 230            22.8 4.0 140.8  95.0  3.9 3.1 22.9 1.0 0.0  4.0  2.0
#> Merc 280            19.2 6.0 167.6 123.0  3.9 3.4 18.3 1.0 0.0  4.0  4.0
#> Merc 280C           17.8 6.0 167.6 123.0  3.9 3.4 18.9 1.0 0.0  4.0  4.0
#> Merc 450SE          16.4 8.0 275.8 180.0  3.1 4.1 17.4 0.0 0.0  3.0  3.0
#> Merc 450SL          17.3 8.0 275.8 180.0  3.1 3.7 17.6 0.0 0.0  3.0  3.0
#> Merc 450SLC         15.2 8.0 275.8 180.0  3.1 3.8 18.0 0.0 0.0  3.0  3.0
#> Cadillac Fleetwood  10.4 8.0 472.0 205.0  2.9 5.2 18.0 0.0 0.0  3.0  4.0
#> Lincoln Continental 10.4 8.0 460.0 215.0  3.0 5.4 17.8 0.0 0.0  3.0  4.0
#> Chrysler Imperial   14.7 8.0 440.0 230.0  3.2 5.3 17.4 0.0 0.0  3.0  4.0
#> Fiat 128            32.4 4.0  78.7  66.0  4.1 2.2 19.5 1.0 1.0  4.0  1.0
#> Honda Civic         30.4 4.0  75.7  52.0  4.9 1.6 18.5 1.0 1.0  4.0  2.0
#> Toyota Corolla      33.9 4.0  71.1  65.0  4.2 1.8 19.9 1.0 1.0  4.0  1.0
#> Toyota Corona       21.5 4.0 120.1  97.0  3.7 2.5 20.0 1.0 0.0  3.0  1.0
#> Dodge Challenger    15.5 8.0 318.0 150.0  2.8 3.5 16.9 0.0 0.0  3.0  2.0
#> AMC Javelin         15.2 8.0 304.0 150.0  3.1 3.4 17.3 0.0 0.0  3.0  2.0
#> Camaro Z28          13.3 8.0 350.0 245.0  3.7 3.8 15.4 0.0 0.0  3.0  4.0
#> Pontiac Firebird    19.2 8.0 400.0 175.0  3.1 3.8 17.1 0.0 0.0  3.0  2.0
#> Fiat X1-9           27.3 4.0  79.0  66.0  4.1 1.9 18.9 1.0 1.0  4.0  1.0
#> Porsche 914-2       26.0 4.0 120.3  91.0  4.4 2.1 16.7 0.0 1.0  5.0  2.0
#> Lotus Europa        30.4 4.0  95.1 113.0  3.8 1.5 16.9 1.0 1.0  5.0  2.0
#> Ford Pantera L      15.8 8.0 351.0 264.0  4.2 3.2 14.5 0.0 1.0  5.0  4.0
#> Ferrari Dino        19.7 6.0 145.0 175.0  3.6 2.8 15.5 0.0 1.0  5.0  6.0
#> Maserati Bora       15.0 8.0 301.0 335.0  3.5 3.6 14.6 0.0 1.0  5.0  8.0
#> Volvo 142E          21.4 4.0 121.0 109.0  4.1 2.8 18.6 1.0 1.0  4.0  2.0

# If we want only want to round some columns
mtcars %>%
  txtRound(wt, qsec_txt = qsec, digits = 1)
#>                      mpg cyl  disp  hp drat  wt  qsec vs am gear carb qsec_txt
#> Mazda RX4           21.0   6 160.0 110 3.90 2.6 16.46  0  1    4    4     16.5
#> Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.9 17.02  0  1    4    4     17.0
#> Datsun 710          22.8   4 108.0  93 3.85 2.3 18.61  1  1    4    1     18.6
#> Hornet 4 Drive      21.4   6 258.0 110 3.08 3.2 19.44  1  0    3    1     19.4
#> Hornet Sportabout   18.7   8 360.0 175 3.15 3.4 17.02  0  0    3    2     17.0
#> Valiant             18.1   6 225.0 105 2.76 3.5 20.22  1  0    3    1     20.2
#> Duster 360          14.3   8 360.0 245 3.21 3.6 15.84  0  0    3    4     15.8
#> Merc 240D           24.4   4 146.7  62 3.69 3.2 20.00  1  0    4    2     20.0
#> Merc 230            22.8   4 140.8  95 3.92 3.1 22.90  1  0    4    2     22.9
#> Merc 280            19.2   6 167.6 123 3.92 3.4 18.30  1  0    4    4     18.3
#> Merc 280C           17.8   6 167.6 123 3.92 3.4 18.90  1  0    4    4     18.9
#> Merc 450SE          16.4   8 275.8 180 3.07 4.1 17.40  0  0    3    3     17.4
#> Merc 450SL          17.3   8 275.8 180 3.07 3.7 17.60  0  0    3    3     17.6
#> Merc 450SLC         15.2   8 275.8 180 3.07 3.8 18.00  0  0    3    3     18.0
#> Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.2 17.98  0  0    3    4     18.0
#> Lincoln Continental 10.4   8 460.0 215 3.00 5.4 17.82  0  0    3    4     17.8
#> Chrysler Imperial   14.7   8 440.0 230 3.23 5.3 17.42  0  0    3    4     17.4
#> Fiat 128            32.4   4  78.7  66 4.08 2.2 19.47  1  1    4    1     19.5
#> Honda Civic         30.4   4  75.7  52 4.93 1.6 18.52  1  1    4    2     18.5
#> Toyota Corolla      33.9   4  71.1  65 4.22 1.8 19.90  1  1    4    1     19.9
#> Toyota Corona       21.5   4 120.1  97 3.70 2.5 20.01  1  0    3    1     20.0
#> Dodge Challenger    15.5   8 318.0 150 2.76 3.5 16.87  0  0    3    2     16.9
#> AMC Javelin         15.2   8 304.0 150 3.15 3.4 17.30  0  0    3    2     17.3
#> Camaro Z28          13.3   8 350.0 245 3.73 3.8 15.41  0  0    3    4     15.4
#> Pontiac Firebird    19.2   8 400.0 175 3.08 3.8 17.05  0  0    3    2     17.1
#> Fiat X1-9           27.3   4  79.0  66 4.08 1.9 18.90  1  1    4    1     18.9
#> Porsche 914-2       26.0   4 120.3  91 4.43 2.1 16.70  0  1    5    2     16.7
#> Lotus Europa        30.4   4  95.1 113 3.77 1.5 16.90  1  1    5    2     16.9
#> Ford Pantera L      15.8   8 351.0 264 4.22 3.2 14.50  0  1    5    4     14.5
#> Ferrari Dino        19.7   6 145.0 175 3.62 2.8 15.50  0  1    5    6     15.5
#> Maserati Bora       15.0   8 301.0 335 3.54 3.6 14.60  0  1    5    8     14.6
#> Volvo 142E          21.4   4 121.0 109 4.11 2.8 18.60  1  1    4    2     18.6