How can I reset factors/levels of a vector after modify strings?
library(stringr)
x <- c(" x1", "x1", "x2 ", " x2", "x1 ", "x2") # Whitespace left or right
as.character(x)
[1] " x1" "x1" "x2 " " x2" "x1 " "x2"
str_replace_all(x, fixed(" "), "")
[1] "x1" "x1" "x2" "x2" "x1" "x2"
factor(x)
[1] x1 x1 x2 x2 x1 x2
Levels: x1 x2 x1 x1 x2 x2`
I would like a result like:
[1] x1 x1 x2 x2 x1 x2
Levels: x1 x2