fast2waytable: Fast Two-Way Contingency Tables

View source: R/utils.R

fast2waytableR Documentation

Fast Two-Way Contingency Tables

Description

Contingency tables of two vectors in a much faster way than the default table.

Usage

fast2waytable(x, y, levels = NULL)

Arguments

x, y

two vectors of the same length with integers (or values that can be coerced to).

levels

the unique values found in both vectors.

Details

If levels is known (and given), the running times are considerably shorter (up to several ten times, see examples).

NA's are not handled in this version.

Value

a square matrix with the counts.

Author(s)

Emmanuel Paradis

See Also

table, tabulate

Examples

z <- 11:20 * 10L
n <- 13e6
x <- sample(z, n, TRUE)
y <- sample(z, n, TRUE)
system.time(res1 <- fast2waytable(x, y)) # ~ 0.4 sec
system.time(res2 <- fast2waytable(x, y, z)) # ~ 0.02 sec
system.time(res3 <- table(x, y)) # ~ 1.8 sec
all(res1 == res2)
all(res1 == res3)

emmanuelparadis/sentinel documentation built on May 2, 2024, 4:52 a.m.