How can I drop unused levels from a data frame?
Solution 1:
There's a recently added function in R for this:
y <- droplevels(y)
Solution 2:
Just do y$let <- factor(y$let)
. Running factor
on an existing factor variable will reset the levels to only those that are present.
Solution 3:
Adding to Hong Ooi's answer, here is an example I found from R-Bloggers.
# Create some fake data
x <- as.factor(sample(head(colors()),100,replace=TRUE))
levels(x)
x <- x[x!="aliceblue"]
levels(x) # still the same levels
table(x) # even though one level has 0 entries!
The solution is simple: run factor() again:
x <- factor(x)
levels(x)