Learn R Programming

quanteda (version 4.5.0)

dfm_match: 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"))

Value

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

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().

See Also

dfm_select()

Examples

Run this code
# 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))

Run the code above in your browser using DataLab