canonicalize_pairs: Canonicalize element pairs

View source: R/transformations.R

canonicalize_pairsR Documentation

Canonicalize element pairs

Description

Coerce a collection of element pairs into canonical form. Facilitates testing of equivalence.

Usage

canonicalize_pairs(pairs, ordered = FALSE)

Arguments

pairs

a matrix or data.frame of element pairs where rows correspond to element pairs and columns correspond to element identifiers.

ordered

whether to treat the element pairs as ordered—i.e. whether pair (x, y) is distinct from pair (y, x) for x \neq y. Defaults to FALSE, which is appropriate for clustering, undirected link prediction, record linkage etc.

Value

Returns the element pairs in canonical form, so that:

  • the first element id precedes the second element id lexicographically if ordered = FALSE—i.e. pair (3, 2) becomes pair (2, 3);

  • pairs with missing element ids are removed;

  • duplicate pairs are removed; and

  • the rows in the matrix/data.frame pairs are sorted lexicographically by the first element id, then by the second element id.

Examples

messy_pairs <- rbind(c(2,1), c(1,2), c(3,1), c(1,2))
clean_pairs <- canonicalize_pairs(messy_pairs)
all(rbind(c(1,2), c(1,3)) == clean_pairs) # duplicates removed and order fixed


clevr documentation built on Sept. 16, 2023, 5:06 p.m.