Fast droplevels
fdroplevels.Rd
Similar to base::droplevels
but much faster.
Arguments
- x
factor
ordata.table
where unused levels should be dropped.- exclude
A
character
vector of factor levels which are dropped no matter of presented or not.- except
An
integer
vector of indices of data.table columns which are not modified by dropping levels.- in.place
logical (default is
FALSE
). IfTRUE
levels of factors ofdata.table
are modified in-place.- ...
further arguments passed to methods
Value
fdroplevels
returns a factor
.
droplevels
returns a data.table
where levels are dropped at factor columns.
Examples
# on vectors
x = factor(letters[1:10])
fdroplevels(x[1:5])
#> [1] a b c d e
#> Levels: a b c d e
# exclude levels from drop
fdroplevels(x[1:5], exclude = c("a", "c"))
#> [1] <NA> b <NA> d e
#> Levels: b d e
# on data.table
DT = data.table(a = factor(1:10), b = factor(letters[1:10]))
droplevels(head(DT))[["b"]]
#> [1] a b c d e f
#> Levels: a b c d e f
# exclude levels
droplevels(head(DT), exclude = c("b", "c"))[["b"]]
#> [1] a <NA> <NA> d e f
#> Levels: a d e f
# except columns from drop
droplevels(head(DT), except = 2)[["b"]]
#> [1] a b c d e f
#> Levels: a b c d e f g h i j
droplevels(head(DT), except = 1)[["b"]]
#> [1] a b c d e f
#> Levels: a b c d e f