Two teams do not quite the same, but not quite, especially if you have the original order of factors. In some cases, you cannot use: as.factor(as.character(f)) . Cm:
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))") fd <- drop.levels(f) plot(fd, main="drop.levels(f)") fd <- drop.levels(f, reorder=FALSE) plot(fd, main="drop.levels(f, reorder=FALSE))") ff <- factor(f) plot(ff, main="factor(f)")

as.factor(as.character(f)) and drop.levels(f) do the same, and they donβt preserve the original order of the factors, they both redefine the text in ABC order. I want to keep the order in which you can use the reorder=FALSE option in drop.levels() .
This is the default behavior of factor() .
source share