as question, as I can see the speed is higher for the later method, why use the first one? Thanks.
The two command does quite the same but not exactly, especially when you have the preserve the original order of the factors. In some cases you cannot use: as.factor(as.character(f))
. See:
par(mfrow=c(2,3))
f <- factor(c("D", "B", "C", "K", "A"), levels=c("K", "B", "C", "D"))[2:4]
plot(f, main="Original factor")
f.fc <- as.factor(as.character(f))
plot(f.fc, main="as.factor(as.character(f))")
f.d <- drop.levels(f)
plot(f.d, main="drop.levels(f)")
f.d <- drop.levels(f, reorder=FALSE)
plot(f.d, main="drop.levels(f, reorder=FALSE))")
f.f <- factor(f)
plot(f.f, main="factor(f)")
as.factor(as.character(f))
and drop.levels(f)
does the same and they do not preserve the original order of factors, they both re-level the text in ABC order. I you want to preserve the order you can use the reorder=FALSE
option in drop.levels()
.
This is the default behavior of factor()
.