A substantially faster replacement for droplevels.

fdroplevels(x, ...)

# S3 method for factor
fdroplevels(x, ...)

# S3 method for data.frame
fdroplevels(x, ...)

Arguments

x

a factor, or data frame / list containing one or more factors.

...

not used.

Details

droplevels passes a factor from which levels are to be dropped to factor, which first calls unique and then match to drop unused levels. Both functions internally use a hash table, which is highly inefficient. fdroplevels does not require mapping values at all, but uses a super fast boolean vector method to determine which levels are unused and remove those levels. In addition, if no unused levels are found, x is simply returned. Any missing values found in x are efficiently skipped in the process of checking and replacing levels. All other attributes of x are preserved.

Value

x will any unused factor levels removed.

Note

If x is malformed i.e. has too few levels, this function can cause a segmentation fault, thus only use with ordinary / proper factors.

See also

Examples

f <- iris$Species[1:100] fdroplevels(f)
#> [1] setosa setosa setosa setosa setosa setosa #> [7] setosa setosa setosa setosa setosa setosa #> [13] setosa setosa setosa setosa setosa setosa #> [19] setosa setosa setosa setosa setosa setosa #> [25] setosa setosa setosa setosa setosa setosa #> [31] setosa setosa setosa setosa setosa setosa #> [37] setosa setosa setosa setosa setosa setosa #> [43] setosa setosa setosa setosa setosa setosa #> [49] setosa setosa versicolor versicolor versicolor versicolor #> [55] versicolor versicolor versicolor versicolor versicolor versicolor #> [61] versicolor versicolor versicolor versicolor versicolor versicolor #> [67] versicolor versicolor versicolor versicolor versicolor versicolor #> [73] versicolor versicolor versicolor versicolor versicolor versicolor #> [79] versicolor versicolor versicolor versicolor versicolor versicolor #> [85] versicolor versicolor versicolor versicolor versicolor versicolor #> [91] versicolor versicolor versicolor versicolor versicolor versicolor #> [97] versicolor versicolor versicolor versicolor #> Levels: setosa versicolor
identical(fdroplevels(f), droplevels(f))
#> [1] TRUE
fNA <- na_insert(f) fdroplevels(fNA)
#> [1] setosa setosa setosa setosa <NA> setosa #> [7] setosa setosa setosa setosa setosa setosa #> [13] setosa setosa setosa setosa setosa setosa #> [19] setosa setosa setosa setosa <NA> setosa #> [25] setosa setosa setosa setosa setosa setosa #> [31] <NA> setosa setosa <NA> setosa <NA> #> [37] setosa setosa setosa setosa setosa setosa #> [43] <NA> setosa setosa <NA> setosa setosa #> [49] setosa setosa versicolor versicolor versicolor versicolor #> [55] versicolor versicolor versicolor versicolor versicolor versicolor #> [61] versicolor versicolor <NA> versicolor versicolor versicolor #> [67] versicolor <NA> versicolor versicolor versicolor versicolor #> [73] versicolor versicolor versicolor versicolor <NA> versicolor #> [79] versicolor versicolor versicolor versicolor versicolor versicolor #> [85] versicolor versicolor versicolor versicolor versicolor versicolor #> [91] versicolor versicolor versicolor versicolor versicolor versicolor #> [97] versicolor versicolor versicolor versicolor #> Levels: setosa versicolor
identical(fdroplevels(fNA), droplevels(fNA))
#> [1] TRUE
identical(fdroplevels(ss(iris, 1:100)), droplevels(ss(iris, 1:100)))
#> [1] TRUE