roundr: roundr

View source: R/utils2.R

roundrR Documentation

roundr

Description

Improved rounding formatter.

Usage

roundr(
  x,
  digits = 1L,
  format = TRUE,
  check = TRUE,
  scipen = getOption("scipen")
)

## Default S3 method:
roundr(
  x,
  digits = 1L,
  format = TRUE,
  check = TRUE,
  scipen = getOption("scipen")
)

## S3 method for class 'matrix'
roundr(
  x,
  digits = 1L,
  format = TRUE,
  check = TRUE,
  scipen = getOption("scipen")
)

## S3 method for class 'data.frame'
roundr(
  x,
  digits = 1L,
  format = TRUE,
  check = TRUE,
  scipen = getOption("scipen")
)

Arguments

x

numeric vector, matrix, or data frame

digits

number of digits past the decimal point to keep

format

logical; if TRUE, large numbers will be formatted with commas

check

logical; if TRUE, formatted strings are checked for consistency with round and warned if not identical

scipen

see options

Details

Uses sprintf to round numeric value while keeping trailing zeros.

Value

An object having the same class as x.

See Also

round; Round; sprintf; round_to; pvalr

Examples

## compare
round(0.199, 2)
roundr(0.199, 2)

round(1e9)
roundr(1e9, 0)
roundr(1e9, 0, scipen = 10)

## drops negative when x rounds to 0, eg, case 1:
roundr(c(1000, 1, -0.0002, 0.0002, 0.5, -0.5, -0.002), digits = 3)

## for matrices or data frames (including factors and/or characters)
roundr(matrix(1:9, 3))

dd <- within(head(mtcars), {
  mpg <- as.character(mpg)
  cyl <- factor(cyl, labels = LETTERS[1:3])
})
roundr(dd)


raredd/rawr documentation built on March 4, 2024, 1:36 a.m.