typecast

Atomic and List Type Casting With Names and Dimensions Preserved

Description

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

The functions are as follows:

  • as_lgl(): 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().
as_bool() is an alias for as_lgl().

See also typeof.

Usage

as_lgl(x, ...)

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

library("broadcast")


# 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      27       28     8    21   3
#> Feb      15        2    26    11   0
#> Mar      13       12     5     4   7
#> Apr      24       18    22    17  10
#> May      23       20    16    14  -1
#> Jun       1       19     9    25   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_lgl(x)
#>     January February March April   May
#> Jan    TRUE     TRUE  TRUE  TRUE  TRUE
#> Feb    TRUE     TRUE  TRUE  TRUE FALSE
#> Mar    TRUE     TRUE  TRUE  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      27       28     8    21   3
#> Feb      15        2    26    11   0
#> Mar      13       12     5     4   7
#> Apr      24       18    22    17  10
#> May      23       20    16    14  -1
#> Jun       1       19     9    25   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      27       28     8    21   3
#> Feb      15        2    26    11   0
#> Mar      13       12     5     4   7
#> Apr      24       18    22    17  10
#> May      23       20    16    14  -1
#> Jun       1       19     9    25   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 "27"    "28"     "8"   "21"  "3" 
#> Feb "15"    "2"      "26"  "11"  "0" 
#> Mar "13"    "12"     "5"   "4"   "7" 
#> Apr "24"    "18"     "22"  "17"  "10"
#> May "23"    "20"     "16"  "14"  "-1"
#> Jun "1"     "19"     "9"   "25"  "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   27+0i    28+0i  8+0i 21+0i  3+0i
#> Feb   15+0i     2+0i 26+0i 11+0i  0+0i
#> Mar   13+0i    12+0i  5+0i  4+0i  7+0i
#> Apr   24+0i    18+0i 22+0i 17+0i 10+0i
#> May   23+0i    20+0i 16+0i 14+0i -1+0i
#> Jun    1+0i    19+0i  9+0i 25+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)
#>     January February March April May
#> Jan      1b       1c    08    15  03
#> Feb      0f       02    1a    0b  00
#> Mar      0d       0c    05    04  07
#> Apr      18       12    16    11  0a
#> May      17       14    10    0e  00
#> Jun      01       13    09    19  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_lgl(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