These are equivalent to the base functions (e.g. as.logical(), as.list(), etc), but perform coercion rather than conversion. This means they are not generic and will not call S3 conversion methods. They only attempt to coerce the base type of their input. In addition, they have stricter implicit coercion rules and will never attempt any kind of parsing. E.g. they will not try to figure out if a character vector represents integers or booleans. Finally, they have treat attributes consistently, unlike the base R functions: all attributes except names are removed.





as_character(x, encoding = NULL)

as_string(x, encoding = NULL)




An object to coerce to a base type.


If non-null, passed to set_chr_encoding() to add an encoding mark. This is only declarative, no encoding conversion is performed.

Coercion to logical and numeric atomic vectors

  • To logical vectors: Integer and integerish double vectors. See is_integerish().

  • To integer vectors: Logical and integerish double vectors.

  • To double vectors: Logical and integer vectors.

  • To complex vectors: Logical, integer and double vectors.

Coercion to character vectors

as_character() and as_string() have an optional encoding argument to specify the encoding. R uses this information for internal handling of strings and character vectors. Note that this is only declarative, no encoding conversion is attempted. See as_utf8_character() and as_native_character() for coercing to a character vector and attempt encoding conversion.

See also set_chr_encoding() and mut_utf8_locale() for information about encodings and locales in R, and string() and chr() for other ways of creating strings and character vectors.

Note that only as_string() can coerce symbols to a scalar character vector. This makes the code more explicit and adds an extra type check.

Coercion to lists

as_list() only coerces vector and dictionary types (environments are an example of dictionary type). Unlike base::as.list(), as_list() removes all attributes except names.

Effects of removing attributes

A technical side-effect of removing the attributes of the input is that the underlying objects has to be copied. This has no performance implications in the case of lists because this is a shallow copy: only the list structure is copied, not the contents (see duplicate()). However, be aware that atomic vectors containing large amounts of data will have to be copied.

In general, any attribute modification creates a copy, which is why it is better to avoid using attributes with heavy atomic vectors. Uncopyable objects like environments and symbols are an exception to this rule: in this case, attributes modification happens in place and has side-effects.


# Coercing atomic vectors removes attributes with both base R and rlang: x <- structure(TRUE, class = "foo", bar = "baz") as.logical(x)
#> [1] TRUE
# But coercing lists preserves attributes in base R but not rlang: l <- structure(list(TRUE), class = "foo", bar = "baz") as.list(l)
#> [[1]] #> [1] TRUE #> #> attr(,"class") #> [1] "foo" #> attr(,"bar") #> [1] "baz"
#> [[1]] #> [1] TRUE #>
# Implicit conversions are performed in base R but not rlang: as.logical(l)
#> [1] TRUE
not_run({ as_logical(l) }) # Conversion methods are bypassed, making the result of the # coercion more predictable: <- function(x) "wrong" as.list(l)
#> [1] "wrong"
#> [[1]] #> [1] TRUE #>
# The input is never parsed. E.g. character vectors of numbers are # not converted to numeric types: as.integer("33")
#> [1] 33
not_run({ as_integer("33") }) # With base R tools there is no way to convert an environment to a # list without either triggering method dispatch, or changing the # original environment. as_list() makes it easy: x <- structure(as_env(mtcars[1:2]), class = "foobar") as.list.foobar <- function(x) abort("dont call me") as_list(x)
#> $cyl #> [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4 #> #> $mpg #> [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4 #> [16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7 #> [31] 15.0 21.4 #>