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
|
|
|
|
Max value
|
|
|
|
Epsilon: smallest representable addition difference
|
|
|
|
Negative epsilon: smallest representable subtraction difference
|
|
|
|
Missing value (NA)
|
|
|
|
Not a number (NaN)
|
|
|
|
Positive infinity
|
|
|
|
Negative infinity
|
|
|
Create
| Action |
Code |
Details |
|
Empty numeric vector, length 0
|
|
|
Numeric vectors
| Action |
Code |
Details |
|
Numeric vector of zeros, length n
|
|
|
|
Cast to numeric
|
|
|
|
Parse string to numeric
|
|
Parse failures return NA |
Test
| Action |
Code |
Details |
|
Is numeric
|
|
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
|
|
|
|
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
|
|
|
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)
|
|
|
All elements are equal within absolute tolerance t (no NAs present)
|
|
|
|
All elements are equal within absolute tolerance t
|
isTRUE(all.equal(x, y, tolerance = t))
|
|
|
Are all elements whole numbers (no NAs present)
|
|
|
|
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)
|
|
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
|
|
trunc(2.6) = 2, trunc(-2.5) = -3 |
|
Ceiling
|
|
ceiling(2.6) = 3, ceiling(-2.5) = -2 |
|
Truncate to integer
|
|
trunc(2.6) = 2, trunc(-2.5) = -2 |
|
Round to d decimal places
|
|
round(2.6) = 3, round(-2.55, 1) = -2.5 |
|
Round to nearest _p_th power of ten
|
|
round(520, -2) = 500 |
|
Round to _d_th significant digits
|
|
|
Convert
| Action |
Code |
Details |
|
To integer (truncate)
|
|
|
| Action |
Code |
Details |
|
To string
|
|
|
|
Format with thousands separator (comma)
|
|
|
|
Format with d significant digits
|
formatC(x, digits = d, format='fg')
|
|
|
Format as percentage
|
|
|
|
Format with sign prefix
|
formatC(x, format = 'f', flag = '+', drop0trailing = TRUE)
|
|
|
Format with scientific notation
|
|
|
|
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)
|
|