Learn R Programming

htmlTable (version 2.4.3)

txtRound: A convenient rounding function

Description

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.

Usage

txtRound(x, ...)

# S3 method for default 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 table txtRound(x, ...)

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

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

Value

matrix/data.frame

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.

Tidy-select with <code>data.frame</code>

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

Run this code
# Basic usage
txtRound(1.023, digits = 1)
# > "1.0"

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

txtRound(12344, digits = 1, txtInt_args = TRUE)
# > "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"

# Using a data.frame directly
library(magrittr)
data("mtcars")
# If we want to round all the numerical values
mtcars %>%
  txtRound(digits = 1)

# If we want only want to round some columns
mtcars %>%
  txtRound(wt, qsec_txt = qsec, digits = 1)

Run the code above in your browser using DataLab