Inputs a dfm sparsity value from sparsity and formats it for printing in print.dfm.

format_sparsity(x, threshold = 0.01, digits = 3, nsmall = 1)

Arguments

x

input sparsity value, ranging from 0 to 1.0

threshold

value below which the decimal places will be rounded and printed with an inequality

digits

digits input to format

nsmall

nsmall input to format

Value

character value for inserting into the dfm print output

Examples

s <- c(.9, .99, .999, .9999, .99999, .1, .01, .001, .0001, .000001, .0000001, .00000001, .000000000001, sparsity(dfm(data_corpus_inaugural)), sparsity(dfm(data_corpus_irishbudget2010)), .12312431242134) for (i in s) print(paste0(format(i, width = 10), ": ", quanteda:::format_sparsity(i)))
#> [1] " 0.9: (90.0% sparse)" #> [1] " 0.99: (99.0% sparse)" #> [1] " 0.999: (99.9% sparse)" #> [1] " 0.9999: (99.99% sparse)" #> [1] " 0.99999: (>99.99% sparse)" #> [1] " 0.1: (10.0% sparse)" #> [1] " 0.01: (1.0% sparse)" #> [1] " 0.001: (0.1% sparse)" #> [1] " 1e-04: (0.01% sparse)" #> [1] " 1e-06: (<0.01% sparse)" #> [1] " 1e-07: (<0.01% sparse)" #> [1] " 1e-08: (<0.01% sparse)" #> [1] " 1e-12: (<0.01% sparse)" #> [1] " 0.9176184: (91.8% sparse)" #> [1] " 0.8117982: (81.2% sparse)" #> [1] " 0.1231243: (12.3% sparse)"
print(as.dfm(Matrix::rsparsematrix(1000, 1000, density = 0.9999)))
#> Document-feature matrix of: 1,000 documents, 1,000 features (<0.01% sparse).
print(as.dfm(Matrix::rsparsematrix(10000, 10000, density = 0.00001)))
#> Document-feature matrix of: 10,000 documents, 10,000 features (>99.99% sparse).