data.sketches

R-CMD-check coverage

data.sketches provides an R interface to the Apache DataSketches library of streaming algorithms for approximate analytics on data too large to hold or process exactly. Sketches are compact, mergeable summaries that answer queries such as approximate distinct counts, quantiles and ranks, frequent items and point-frequency estimates, weighted sampling, and set membership, within known error bounds.

The package implements, grouped by family:

Quantile sketches for approximate quantiles, ranks, CDF, and PMF:

Cardinality sketches for approximate distinct counting:

Frequency sketches for approximate frequency estimation:

Tuple sketches, a Theta-extension that pairs per-key value arrays with approximate distinct counting:

Sampling sketches for weighted sampling from a stream:

Filters for approximate set membership:

Installation

Install the development version from GitHub with pak:

pak::pak("pedrobtz/data.sketches")

Or install the released version from CRAN with:

install.packages("data.sketches")

Example

Build a KLL sketch from a numeric vector and query approximate quantiles and ranks:

library(data.sketches)

sketch <- kll_doubles(rnorm(10000))
sketch

sketch$quantile(c(0.25, 0.5, 0.75))
sketch$rank(c(-1, 0, 1))

Sketches are mergeable, so partial sketches built from different chunks of a stream can be combined into one:

a <- kll_doubles(rnorm(5000, mean = -2))
b <- kll_doubles(rnorm(5000, mean = 2))
a$merge(b)
a$quantile(c(0.25, 0.5, 0.75))

Sketches can be serialized to a raw vector and restored, for interoperability with other Apache DataSketches implementations:

bytes <- sketch$serialize()
restored <- kll_doubles(bytes = bytes)
restored$quantile(0.5)