dfm_sample: Randomly sample documents or features from a dfm

Description Usage Arguments Value See Also Examples

View source: R/dfm_sample.R

Description

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

Usage

1
2
3
4
5
6
7
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

1
2
3
4
5
6
7
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))

koheiw/quanteda.core documentation built on Sept. 21, 2020, 3:44 p.m.