I have data frame with "State" column, which is a factor with US State.
Not all states are present among values, while all states are among factor levels.
How to find factor levels, which are never used in the data frame?
The number of levels of a factor or independent variable is equal to the number of variations of that factor that were used in the experiment. If an experiment compared the drug dosages 50 mg, 100 mg, and 150 mg, then the factor "drug dosage" would have three levels: 50 mg, 100 mg, and 150 mg.
The droplevels() function in R can be used to drop unused factor levels. This function is particularly useful if we want to drop factor levels that are no longer used due to subsetting a vector or a data frame. where x is an object from which to drop unused factor levels.
droplevels in R with examples, To remove unneeded factor levels, use R's droplevels() function. This function comes in handy when we need to get rid of factor levels that are no longer in use as a result of subsetting a vector or a data frame.
The droplevels R function removes unused levels of a factor. The function is typically applied to vectors or data frames.
Try:
# A toy factor variable:
f <- factor(letters[1:2], levels = letters[1:4])
f
[1] a b
Levels: a b c d
levels(f)
[1] "a" "b" "c" "d"
To see the unused levels:
setdiff(levels(f), f)
[1] "c" "d"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With