How to find unused factor levels?

I have a data frame with a Status column, which is a factor from the USA.

Not all states are present among the meanings, while all states are among the levels of factors.

How to find factor levels that are never used in a data frame?

+5
source share
1 answer

Try:

# A toy factor variable: f <- factor(letters[1:2], levels = letters[1:4]) f [1] ab Levels: abcd levels(f) [1] "a" "b" "c" "d" 

To view unused levels:

 setdiff(levels(f), f) [1] "c" "d" 
+9
source

All Articles