roundr-deprecated: A rounding function with option to return character

roundr-deprecatedR Documentation

A rounding function with option to return character

Description

A simple rounding function that returns exactly the number of digits requested. Really just a wrapper for format and round.

Credit for this one goes to my buddy Kyle Hart. I had seen a few different versions of a function like this and I really like his for the flexibility that it offers by formatting as a character. In the end, I took his basic function and took it a few steps further to be the way that I want it to be, like the option to trim whitespace.

Usage

roundr(x, d, as_text, trim)

Arguments

x

a numeric vector.

d

integer indicating the number of decimal places (round). Negative values are allowed (see 'Examples').

as_text

logical; if FALSE, a numeric value is returned; if TRUE, then a character is returned with values aligned at the decimal point and padded with spaces in front (use trim option to keep or remove spaces)

trim

logical; if FALSE, logical, numeric and complex values are right-justified to a common width; if TRUE the leading blanks for justification are suppressed. Only has an effect if as_text is TRUE.

Details

Rounding to a negative number of digits means rounding to a power of ten.

Value

An object of similar structure to x

See Also

lamisc-deprecated

Examples

numbers <- c(0.0018, 0.1234, 12.6978, -86.75309, 264.4445, -99.9999)
roundr(numbers, d = 2, as_text = TRUE)
roundr(numbers, d = 2, as_text = TRUE, trim = TRUE)
roundr(numbers, d = 2, as_text = FALSE)
roundr(numbers, d = -2, as_text = FALSE)
roundr(numbers, d = -2, as_text = TRUE, trim = TRUE)
roundr(numbers, d = -2, as_text = TRUE, trim = FALSE)

numbers <- c(10.00001, 12345, 1234.5, NA, 1.2345, 0.12345)
roundr(numbers, d = 2, as_text = TRUE)
roundr(numbers, d = 2, as_text = TRUE, trim = TRUE)
roundr(numbers, d = 2, as_text = FALSE)
roundr(numbers, d = -2, as_text = FALSE)
roundr(numbers, d = -2, as_text = TRUE, trim = TRUE)
roundr(numbers, d = -2, as_text = TRUE, trim = FALSE)


emilelatour/lamisc documentation built on April 9, 2024, 10:33 a.m.