View source: R/txtFrmt_round.R
txtRound | R Documentation |
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)
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 |
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 |
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 |
scientific |
If the value should be in scientific format. |
txtInt_args |
A list of arguments to pass to |
excl.cols |
Columns to exclude from the rounding procedure when provided a matrix.
This can be either a number or regular expression. Skipped if |
excl.rows |
Rows to exclude from the rounding procedure when provided a matrix. This can be either a number or regular expression. |
matrix/data.frame
data.frame
The txtRound
can use data.frame
for input. This allows us to use
tidyselect
patterns as popularized by dplyr.
Other text formatters:
txtInt()
,
txtMergeLines()
,
txtPval()
# 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.