Last chance! 50% off unlimited learning
Sale ends in
The function droplevels
is used to drop unused levels from a
factor
or, more commonly, from factors in a data frame.
# S3 method for factor
droplevels(x, exclude = if(anyNA(levels(x))) NULL else NA, …)
# S3 method for data.frame
droplevels(x, except, exclude, …)
an object from which to drop unused factor levels.
passed to factor()
; factor levels which
should be excluded from the result even if present. Note that this
was implicitly NA
in R <= 3.3.1 which did drop
NA
levels even when present in x
, contrary to the
documentation. The current default is compatible with x[ , drop=TRUE]
.
further arguments passed to methods
indices of columns from which not to drop levels
droplevels
returns an object of the same class as x
The method for class "factor"
is currently equivalent to
factor(x, exclude=exclude)
. For the data frame method, you
should rarely specify exclude
“globally” for all factor
columns; rather the default uses the same factor-specific
exclude
as the factor method itself.
The except
argument follow the usual indexing rules.
subset
for subsetting data frames.
factor
for definition of factors.
drop
for dropping array dimensions.
drop1
for dropping terms from a model.
[.factor
for subsetting of factors.
# NOT RUN {
aq <- transform(airquality, Month = factor(Month, labels = month.abb[5:9]))
aq <- subset(aq, Month != "Jul")
table( aq $Month)
table(droplevels(aq)$Month)
# }
Run the code above in your browser using DataLab