Learn R Programming

quanteda (version 1.5.2)

dfm_sample: Randomly sample documents or features from a dfm

Description

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

Usage

dfm_sample(
  x,
  size = ifelse(margin == "documents", ndoc(x), nfeat(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. The default is the number of documents or the number of features, for margin = "documents" and margin = "features" respectively.

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

Run this code
# NOT RUN {
set.seed(10)
dfmat <- dfm(c("a b c c d", "a a c c d d d"))
head(dfmat)
head(dfm_sample(dfmat))
head(dfm_sample(dfmat, replace = TRUE))
head(dfm_sample(dfmat, margin = "features"))
head(dfm_sample(dfmat, margin = "features", replace = TRUE))
# }

Run the code above in your browser using DataLab