atomic_typecast: Atomic Type Casting With Names and Dimensions Preserved

atomic_typecastR Documentation

Atomic Type Casting With Names and Dimensions Preserved

Description

Atomic type casting in R is generally performed using the functions as.logical, as.integer, as.double, as.character, as.complex, and as.raw.

Converting an object between atomic types using these functions strips the object of its attributes, including (dim)names and dimensions.

The functions provided here by the 'tinycodet' package preserve the dimensions, dimnames, and names.

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.

  • as_cplx(): converts object to atomic type complex.

  • as_raw():converts object to atomic type raw.

Usage

as_bool(x, ...)

as_int(x, ...)

as_dbl(x, ...)

as_chr(x, ...)

as_cplx(x, ...)

as_raw(x, ...)

Arguments

x

vector, matrix, array (or a similar object where all elements share the same type).

...

further arguments passed to or from other methods.

Value

The converted object.

See Also

tinycodet_dry

Examples


# matrix example ====
x <- matrix(sample(-1:28), ncol = 5)
colnames(x) <- month.name[1:5]
rownames(x) <- month.abb[1:6]
names(x) <- c(letters[1:20], LETTERS[1:10])
print(x)

as_bool(x)
as_int(x)
as_dbl(x)
as_chr(x)
as_cplx(x)
as_raw(x)


################################################################################

# factor example ====
x <- factor(month.abb, levels = month.abb)
names(x) <- month.name
print(x)

as_bool(as_int(x) > 6)
as_int(x)
as_dbl(x)
as_chr(x)
as_cplx(x)
as_raw(x)

tinycodet documentation built on Sept. 12, 2024, 7:03 a.m.