I'm maintaining some R code and I'm studying this function that basically seems to do the same work at as.vector()
but does it in a really round about obscuring way.
f2v <- function(x) if ( is.factor(x) ) levels(x)[x] else x
And one of the usages of the function just appears to unnecessarily invoke it.
xx <- as.factor(f2v(x))
I'm still pretty fresh to R so I'm just wondering if I'm missing some subtle point here that I'm failing to appreciate. But on the surface it looks like the author didn't really understand what they were doing, maybe copy pasting things blindly.
This will re-order the levels of your factor and drop missing levels:
x <- factor(letters[1:6], levels=rev(letters))
x
# [1] a b c d e f
# Levels: z y x w v u t s r q p o n m l k j i h g f e d c b a
as.factor(f2v(x))
# [1] a b c d e f
# Levels: a b c d e f
as.factor(x)
# [1] a b c d e f
# Levels: z y x w v u t s r q p o n m l k j i h g f e d c b a
While this may seem like a subtle distinction, it will impact applications that use factor levels (e.g. ggplot
for ordering categorical variables). It also seems like a bad idea to randomly drop and re-order factor levels. Those levels are typically meaningful and should be accounted for properly.