atomic_conversions {tinycodet} | R Documentation |
Atomic type casting in R is generally performed using the functions
as.logical, as.integer, as.double,
as.character.
Converting an object between atomic types using these functions
strips the object of its attributes,
including attributes such as names and dimensions.
The functions provided here by the 'tinycodet' package
preserve all attributes - except the "class" attribute.
The functions are as follows:
as_bool()
: converts object to atomic type logical
(TRUE, FALSE, NA
).
as_int()
: converts object to atomic type integer
.
as_dbl()
: converts object to atomic type double
(AKA decimal numbers).
as_chr()
: converts object to atomic type character
.
Moreover, the function is_wholenumber()
is added, to safely test for whole numbers.
as_bool(x, ...)
as_int(x, ...)
as_dbl(x, ...)
as_chr(x, ...)
is_wholenumber(x, tol = sqrt(.Machine$double.eps))
x |
vector, matrix, array (or a similar object where all elements share the same type). |
... |
further arguments passed to or from other methods. |
tol |
numeric, giving the tolerance. |
The converted object.
x <- c(rep(0, 2), seq(0, 2.5, by=0.5)) |> matrix(ncol=2)
colnames(x) <- c("one", "two")
attr(x, "test") <- "test"
print(x)
# notice that in all following, attributes (except class) are conserved:
as_bool(x)
as_int(x)
as_dbl(x)
as_chr(x)
# is_wholenumber:
is_wholenumber(1:10 + c(0, 0.1))