Skip to contents

Concise aliases for common base R coercion functions. num(), int(), chr() wrap as.numeric(), as.integer(), and as.character(), but keep the shape of x. nchr() handles the common pitfall of coercing factors to numeric. bin() converts a binary vector to logical.

Usage

num(x, ...)

int(x, ...)

chr(x, ...)

nchr(x)

bin(x, ..., warn = FALSE)

Arguments

x

a vector. For bin(), any input asBinary() accepts.

...

further arguments passed to the underlying base function (as.numeric, as.integer, as.character, or asBinary()).

warn

logical, passed to asBinary(). FALSE (default) suppresses its warning.

Value

a vector, matrix or array of the target type with the names, dim and dimnames of x; for bin(), a logical vector of the same length as x.

Details

The base coercion functions drop all attributes, so a matrix comes back as a plain vector. num(), int(), chr() and nchr() change only the type: names, and dim and dimnames of a matrix, array or table, are kept. The class is not (a table becomes a matrix or array), nor are other attributes. This holds for conversions that keep the length; if a class-specific method returns a different length, its result is passed on unchanged.

num(x, ...)

as.numeric(x), keeping the shape. A factor gives its integer codes, as in base R; use nchr() for the values of the labels.

int(x, ...)

as.integer(x), keeping the shape.

chr(x, ...)

as.character(x), keeping the shape.

nchr(x)

numeric values of x, keeping the shape. For a factor these are the values of its labels, avoiding the trap of as.numeric(factor) returning the internal integer codes; any other input goes to as.numeric() directly, so numbers keep their full precision and logicals become 0/1, which a detour through as.character() would lose.

bin(x, ...)

as.logical(asBinary(x)): 1 becomes TRUE, 0 FALSE. Which input is accepted and which value is coded as 1 is decided by asBinary(), see there. To reverse, name the value that becomes TRUE with pos (passed to asBinary()): bin(x, pos = "no") turns the values and the "coding" attribute together. !bin(x) would turn the values only and leave coding describing the opposite mapping.

See also

Examples

num("3.14")
#> [1] 3.14
num(c(a = "1", b = "2"))                # names kept
#> a b 
#> 1 2 
str(num(VADeaths))                      # still a 5 x 4 matrix
#>  num [1:5, 1:4] 11.7 18.1 26.9 41 66 8.7 11.7 20.3 30.9 54.3 ...
#>  - attr(*, "dimnames")=List of 2
#>   ..$ : chr [1:5] "50-54" "55-59" "60-64" "65-69" ...
#>   ..$ : chr [1:4] "Rural Male" "Rural Female" "Urban Male" "Urban Female"
int(table(c(1, 1, 2)))                  # dims kept, class dropped
#> 
#> 1 2 
#> 2 1 
int(3.9)                               # truncates, does not round
#> [1] 3
chr(1:3)
#> [1] "1" "2" "3"
nchr(factor(c("1.5", "2.0", "1.5"))) # correct: 1.5 2.0 1.5
#> [1] 1.5 2.0 1.5
as.numeric(factor(c("1.5", "2.0")))  # wrong:   1   2
#> [1] 1 2

bin(c(0L, 1L, 0L, 1L))
#> [1] FALSE  TRUE FALSE  TRUE
bin(c("no", "yes", "no"))            # "no" -> FALSE, "yes" -> TRUE
#> [1] FALSE  TRUE FALSE
#> attr(,"coding")
#>  no yes 
#>   0   1 
bin(c("no", "yes", "no"), pos = "no") # reversed: "no" -> TRUE
#> [1]  TRUE FALSE  TRUE
#> attr(,"coding")
#> yes  no 
#>   0   1 
bin(factor(c("m", "w", "m")))        # "m" -> FALSE, "w" -> TRUE
#> [1] FALSE  TRUE FALSE
#> attr(,"coding")
#> m w 
#> 0 1