Sample randomly from a dfm object, from documents or features.

dfm_sample(x, size = ndoc(x), replace = FALSE, prob = NULL,
  margin = c("documents", "features"))

Arguments

x

the dfm object whose documents or features will be sampled

size

a positive number, the number of documents or features to select

replace

logical; should sampling be with replacement?

prob

a vector of probability weights for obtaining the elements of the vector being sampled.

margin

dimension (of a dfm) to sample: can be documents or features

Value

A dfm object with number of documents or features equal to size, drawn from the dfm x.

See also

sample

Examples

set.seed(10) myDfm <- dfm(data_corpus_inaugural[1:10]) head(myDfm)
#> Document-feature matrix of: 6 documents, 3,366 features (82.7% sparse).
head(dfm_sample(myDfm))
#> Document-feature matrix of: 6 documents, 3,366 features (82.7% sparse).
head(dfm_sample(myDfm, replace = TRUE))
#> Document-feature matrix of: 6 documents, 3,366 features (84.5% sparse).
head(dfm_sample(myDfm, margin = "features"))
#> Document-feature matrix of: 6 documents, 10 features (90% sparse). #> 6 x 10 sparse Matrix of class "dfm" #> features #> docs reciprocated savage accomplishing anxious representative vows #> 1789-Washington 0 0 0 0 0 0 #> 1793-Washington 0 0 0 0 0 0 #> 1797-Adams 0 0 0 0 0 0 #> 1801-Jefferson 0 0 0 1 1 0 #> 1805-Jefferson 0 0 0 0 0 0 #> 1809-Madison 0 1 0 0 0 0 #> features #> docs observed situation enhance approach #> 1789-Washington 0 0 0 0 #> 1793-Washington 0 0 0 0 #> 1797-Adams 0 1 0 0 #> 1801-Jefferson 0 0 0 1 #> 1805-Jefferson 0 0 0 0 #> 1809-Madison 0 1 0 0