4

as question, as I can see the speed is higher for the later method, why use the first one? Thanks.

joran
  • 169,992
  • 32
  • 429
  • 468
lokheart
  • 23,743
  • 39
  • 98
  • 169

3 Answers3

2

If you are trying to drop unused levels, all you need to do is:

x <- factor(x) 
IRTFM
  • 258,963
  • 21
  • 364
  • 487
2

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.

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
2

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)")

alt text

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().

daroczig
  • 28,004
  • 7
  • 90
  • 124