fdroplevels: Fast droplevels

View source: R/fdroplevels.R

fdroplevelsR Documentation

Fast droplevels

Description

Similar to base::droplevels but much faster.

Usage

fdroplevels(x, exclude = if (anyNA(levels(x))) NULL else NA, ...)
setdroplevels(x, except = NULL, exclude = NULL)

## S3 method for class 'data.table'
droplevels(x, except = NULL, exclude, in.place = FALSE, ...)

Arguments

x

factor or data.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). If TRUE levels of factors of data.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.

See Also

data.table, duplicated, unique

Examples

# on vectors
x = factor(letters[1:10])
fdroplevels(x[1:5])
# exclude levels from drop
fdroplevels(x[1:5], exclude = c("a", "c"))

# on data.table
DT = data.table(a = factor(1:10), b = factor(letters[1:10]))
droplevels(head(DT))[["b"]]
# exclude levels
droplevels(head(DT), exclude = c("b", "c"))[["b"]]
# except columns from drop
droplevels(head(DT), except = 2)[["b"]]
droplevels(head(DT), except = 1)[["b"]]

data.table documentation built on Oct. 10, 2024, 5:07 p.m.