dfm_match: Match the dfm columns with given features

View source: R/dfm_match.R

dfm_matchR Documentation

Match the dfm columns with given features

Description

Match the columns of multiple dfm objects using a character vector.

Usage

dfm_match(x, features, verbose = quanteda_options("verbose"))

Arguments

x

the dfm object.

features

character vector for the feature names to be matched in the resulting dfm. Columns not included in features are removed.

verbose

if TRUE print the number of tokens and documents before and after the function is applied. The number of tokens does not include paddings.

Details

Matching the dfm columns is necessary when you split a corpus into two: you fit a model on the test set and evaluation it on the test set whose features must be identical. It is also used in bootstrap_dfm().

Value

A dfm whose features are identical to those specified in features.

Note

Unlike dfm_select(), this function will add feature names not already present in x. It also provides only fixed, case-sensitive matches. For more flexible feature selection, see dfm_select().

See Also

dfm_select()

Examples

# matching a dfm to a feature vector
dfm_match(dfm(tokens("")), letters[1:5])
dfm_match(data_dfm_lbgexample, c("A", "B", "Z"))
dfm_match(data_dfm_lbgexample, c("B", "newfeat1", "A", "newfeat2"))

# matching one dfm to another
txt <- c("This is text one", "The text two", "This is text three")
(dfmt1 <- dfm(tokens(txt[1:2])))
(dfmt2 <- dfm(tokens(txt[2:3])))
(dfmt3 <- dfm(dfm_match(dfmt1, featnames(dfmt2))))
identical(featnames(dfmt2), featnames(dfmt3))

quanteda documentation built on Aug. 5, 2026, 1:09 a.m.