typecast: Atomic and List Type Casting With Names and Dimensions...

typecastR Documentation

Atomic and List Type Casting With Names and Dimensions Preserved

Description

Type casting usually strips away attributes of objects.
The functions provided here preserve dimensions, dimnames, and names, which may be more convenient for arrays and array-like objects.

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 numeric).

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

  • as_chr(): converts object to atomic type character.

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

  • as_list(): converts object to recursive type list.

as_num() is an alias for as_dbl().
as_str() is an alias for as_chr().

See also typeof.

Usage

as_bool(x, ...)

as_int(x, ...)

as_dbl(x, ...)

as_num(x, ...)

as_chr(x, ...)

as_str(x, ...)

as_cplx(x, ...)

as_raw(x, ...)

as_list(x, ...)

Arguments

x

an R object.

...

further arguments passed to or from other methods.

Value

The converted object.

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)

broadcast documentation built on Sept. 15, 2025, 5:08 p.m.