Suppose I have this object, which is the dput () form of an invalid coefficient (for example, printing will complain about duplicate level 3):
x <- structure(c(1L, 2L, 3L, 4L), .Label = c("A", "B", "A", "C"),
class = "factor")
What is the best way using only the base R to convert it to a real factor
structure(c(1L, 2L, 1L, 3L), .Label = c("A", "B", "C"), class = "factor")
I managed to come up with
factor(levels(x)[x])
but I'm not sure that this will continue to work in the future without warning, and it is probably also very inefficient (the object of the real factor that I am trying to recover is huge).
source
share