as question, as I can see the speed is higher for the later method, why use the first one? Thanks.
3 Answers
If you are trying to drop unused levels, all you need to do is:
x <- factor(x)

- 258,963
- 21
- 364
- 487
-
so factor(x) is equivalent to as.factor(as.character(x))? – lokheart Jan 17 '11 at 07:20
-
Yes, factor(x) will have the same result. – IRTFM Jan 17 '11 at 07:32
-
@lokheart: I think they will not have the same result if you have to preserve the order of your factors. – daroczig Jan 17 '11 at 10:43
New in R (from version 2.12.0) is the function droplevels()
to do the same thing. It is implemented as:
> base:::droplevels.factor
function (x, ...)
factor(x)
<environment: namespace:base>
So I would use that function out of preference. It is a generic function in R with methods for objects of class "factor"
and "data.frame"
, the latter is useful when there are many factors in the data frame that need levels dropping.

- 170,508
- 25
- 396
- 453
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()
.

- 28,004
- 7
- 90
- 124