How can I drop unused levels from a data frame?
问题 Given the following mock data: set.seed(123) x <- data.frame(let = sample(letters[1:5], 100, replace = T), num = sample(1:10, 100, replace = T)) y <- subset(x, let != 'a') Creating a table of y$let yields a b c d e 0 20 21 22 18 But I don't want a to show anymore. If I try to do this: levels(y$let) <- factor(y$let) I mess the frequencies, since now table(y$let) gives me b d c e 0 20 21 40 I'm aware I could do xtabs(~ y$let, drop.unused.levels = T) and work around the problem, but it doesn't