Can't convert a factor to a character vector with rlang

Viewed 317

When trying to convert a factor to a character with as_character(), I get an error.

a = factor(c(1,2))
a %>% as_character()
Error: Can't convert a `factor` object to a character vector

The base equivalent works:

a %>% as.character()
[1] "1" "2"

Why can't I make this simple operation with as_character?

1 Answers

This is because using as_character performs coercion and not conversion. Whereas, if you had used as.character, it would have done type conversion.

You can check the documentation of the as_character function by typing ?as_character`. This will give you more details.

Related