Skip to content
Incomplete sheet

This sheet is incomplete and could use some attention. Please submit code snippet suggestions as an issue or PR here.

numeric

For representing numeric values as doubles

Constants

Action Code Details
Min value
.Machine$double.xmin
Max value
.Machine$double.xmax
Epsilon: smallest representable addition difference
.Machine$double.eps
Negative epsilon: smallest representable subtraction difference
.Machine$double.neg.eps
Missing value (NA)
NA_real_
Not a number (NaN)
NaN
Positive infinity
Inf
Negative infinity
-Inf

Create

Action Code Details
Empty numeric vector, length 0
numeric()

Numeric vectors

Action Code Details
Numeric vector of zeros, length n
numeric(n)
Cast to numeric
as.numeric(y)
Parse string to numeric
as.numeric(y)
Parse failures return NA

Test

Action Code Details
Is numeric
is.numeric(x)
Integers are also numeric
Is numeric, but not integer
storage.mode(x) == 'double'

Tests for single number(s)

Action Code Details
Is equal to number
isTRUE(all.equal(x, y))
Is equal to number
abs(x - y) < .Machine$double.eps
Is equal to number with tolerance t
isTRUE(all.equal(x, y, tolerance = t))
Is equal to number with tolerance t
abs(x - y) < t

Tests for vector

Action Code Details
All elements are equal (no NAs present)
abs(max(x) - min(x)) < .Machine$double.neg.eps
All elements are equal (with NAs)
isTRUE(all.equal(x, y))
All elements are equal within absolute tolerance t (no NAs present)
abs(max(x) - min(x)) < t
All elements are equal within absolute tolerance t
isTRUE(all.equal(x, y, tolerance = t))
Are all elements whole numbers (no NAs present)
all(x == round(x))
Are all elements whole numbers (no NAs present)
max(x %% 1) < .Machine$double.neg.eps
Are all elements whole numbers?
max(x %% 1, na.rm = TRUE) < .Machine$double.neg.eps
Are all elements rounded to at most p decimals? (no NAs present)
all(x == round(x, p))
For example, for p=1: 0.1, -2.6, for p=2: 0.11, -2.63
Are all elements rounded to at most p decimals? (no NAs present)
all(x == round(x, p), na.rm = TRUE)

Derive

Action Code Details
Floor
floor(x)
trunc(2.6) = 2, trunc(-2.5) = -3
Ceiling
ceiling(x)
ceiling(2.6) = 3, ceiling(-2.5) = -2
Truncate to integer
trunc(x)
trunc(2.6) = 2, trunc(-2.5) = -2
Round to d decimal places
round(x, d)
round(2.6) = 3, round(-2.55, 1) = -2.5
Round to nearest _p_th power of ten
round(x, -p)
round(520, -2) = 500
Round to _d_th significant digits
signif(x, d)

Convert

Action Code Details
To integer (truncate)
as.integer(x)

Format as string

Action Code Details
To string
as.character(x)
Format with thousands separator (comma)
scales::comma(x)
Format with d significant digits
formatC(x, digits = d, format='fg')
Format as percentage
scales::percent(x)
Format with sign prefix
formatC(x, format = 'f', flag = '+', drop0trailing = TRUE)
Format with scientific notation
formatC(x, format = 'e')
Format with scientific notation and sign prefix
formatC(x, format = 'e', flag = '+')
Format with scientific notation, with d significant digits
formatC(x, format = 'e', digits = d)