Atomic Type Casting With Names and Dimensions Preserved
Source:R/atomic_typecast.R
atomic_typecast.Rd
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 typelogical
(TRUE, FALSE, NA
).as_int()
: converts object to atomic typeinteger
.as_dbl()
: converts object to atomic typedouble
(AKA decimal numbers).as_chr()
: converts object to atomic typecharacter
.as_cplx()
: converts object to atomic typecomplex
.as_raw()
:converts object to atomic typeraw
.
Arguments
- x
vector, matrix, array (or a similar object where all elements share the same type).
- ...
further arguments passed to or from other methods.
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)
#> January February March April May
#> Jan 11 23 22 15 27
#> Feb 21 2 28 17 19
#> Mar 10 24 0 26 1
#> Apr 3 7 12 14 -1
#> May 4 25 5 20 8
#> Jun 13 18 9 16 6
#> attr(,"names")
#> [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
#> [20] "t" "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
as_bool(x)
#> January February March April May
#> Jan TRUE TRUE TRUE TRUE TRUE
#> Feb TRUE TRUE TRUE TRUE TRUE
#> Mar TRUE TRUE FALSE TRUE TRUE
#> Apr TRUE TRUE TRUE TRUE TRUE
#> May TRUE TRUE TRUE TRUE TRUE
#> Jun TRUE TRUE TRUE TRUE TRUE
#> attr(,"names")
#> [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
#> [20] "t" "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
as_int(x)
#> January February March April May
#> Jan 11 23 22 15 27
#> Feb 21 2 28 17 19
#> Mar 10 24 0 26 1
#> Apr 3 7 12 14 -1
#> May 4 25 5 20 8
#> Jun 13 18 9 16 6
#> attr(,"names")
#> [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
#> [20] "t" "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
as_dbl(x)
#> January February March April May
#> Jan 11 23 22 15 27
#> Feb 21 2 28 17 19
#> Mar 10 24 0 26 1
#> Apr 3 7 12 14 -1
#> May 4 25 5 20 8
#> Jun 13 18 9 16 6
#> attr(,"names")
#> [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
#> [20] "t" "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
as_chr(x)
#> January February March April May
#> Jan "11" "23" "22" "15" "27"
#> Feb "21" "2" "28" "17" "19"
#> Mar "10" "24" "0" "26" "1"
#> Apr "3" "7" "12" "14" "-1"
#> May "4" "25" "5" "20" "8"
#> Jun "13" "18" "9" "16" "6"
#> attr(,"names")
#> [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
#> [20] "t" "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
as_cplx(x)
#> January February March April May
#> Jan 11+0i 23+0i 22+0i 15+0i 27+0i
#> Feb 21+0i 2+0i 28+0i 17+0i 19+0i
#> Mar 10+0i 24+0i 0+0i 26+0i 1+0i
#> Apr 3+0i 7+0i 12+0i 14+0i -1+0i
#> May 4+0i 25+0i 5+0i 20+0i 8+0i
#> Jun 13+0i 18+0i 9+0i 16+0i 6+0i
#> attr(,"names")
#> [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
#> [20] "t" "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
as_raw(x)
#> Warning: out-of-range values treated as 0 in coercion to raw
#> January February March April May
#> Jan 0b 17 16 0f 1b
#> Feb 15 02 1c 11 13
#> Mar 0a 18 00 1a 01
#> Apr 03 07 0c 0e 00
#> May 04 19 05 14 08
#> Jun 0d 12 09 10 06
#> attr(,"names")
#> [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
#> [20] "t" "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
################################################################################
# factor example ====
x <- factor(month.abb, levels = month.abb)
names(x) <- month.name
print(x)
#> January February March April May June July August
#> Jan Feb Mar Apr May Jun Jul Aug
#> September October November December
#> Sep Oct Nov Dec
#> Levels: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
as_bool(as_int(x) > 6)
#> January February March April May June July August
#> FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE
#> September October November December
#> TRUE TRUE TRUE TRUE
as_int(x)
#> January February March April May June July August
#> 1 2 3 4 5 6 7 8
#> September October November December
#> 9 10 11 12
as_dbl(x)
#> January February March April May June July August
#> 1 2 3 4 5 6 7 8
#> September October November December
#> 9 10 11 12
as_chr(x)
#> January February March April May June July August
#> "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug"
#> September October November December
#> "Sep" "Oct" "Nov" "Dec"
as_cplx(x)
#> January February March April May June July August
#> 1+0i 2+0i 3+0i 4+0i 5+0i 6+0i 7+0i 8+0i
#> September October November December
#> 9+0i 10+0i 11+0i 12+0i
as_raw(x)
#> January February March April May June July August
#> 01 02 03 04 05 06 07 08
#> September October November December
#> 09 0a 0b 0c