How to convert a chr variable to a factor variable to be able to identify levels

You can use as.factor() function

x <- c("a", "b", "c")
class(x)
#> [1] "character"
x <- as.factor(x)
class(x)
#> [1] "factor"
x
#> [1] a b c
#> Levels: a b c
levels(x)
#> [1] "a" "b" "c"

Created on 2019-03-06 by the reprex package (v0.2.1)

If you need more specific help, please make your questions with a minimal REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.

If you've never heard of a reprex before, you might want to start by reading this FAQ:

1 Like