Learn R Programming

fctutils (version 0.0.7)

ft_pairs: Generate Pairwise Comparisons Between Elements in factor

Description

Creates all unique pairwise combinations between factor_vec of a vector, with options for references, symmetry, NA handling, custom filtering, and output formats. Automatically handles factors by converting them to vectors and removes extra spaces from factor_vec before processing.

Usage

ft_pairs(
  factor_vec,
  ref = NULL,
  symmetric = TRUE,
  include_na = FALSE,
  include_self = FALSE,
  filter_fn = NULL,
  pre_fn = trimws,
  sort_by = "none",
  output_format = "data.frame"
)

Value

A data frame, list, matrix, or tibble containing pairwise comparisons between factor_vec.

Arguments

factor_vec

A vector containing the factor_vec to compare. Can be of any type (character, numeric, factor, etc.).

ref

Optional. A vector containing the reference factor_vec. If NULL (default), comparisons are made within the factor_vec vector.

symmetric

Logical. If TRUE (default), unique unordered pairs are returned. If FALSE, all ordered pairs are returned.

include_na

Logical. If FALSE (default), NA values are excluded from comparisons. If TRUE, NA values are included.

include_self

Logical. If FALSE (default), pairs where Var1 == Var2 are excluded. If TRUE, they are included.

filter_fn

Optional. A custom function to filter the pairs. Should accept a data frame and return a logical vector.

pre_fn

Optional. A function to preprocess the factor_vec before comparison (e.g., tolower, trimws). Default is trimws.

sort_by

Character string specifying how to sort the output. Options are "Var1", "Var2", "both", or "none" (default).

output_format

Character string specifying the output format. Options are "data.frame" (default), "list", or "matrix".

Examples

Run this code
# Example vector with extra spaces and mixed case
vec <- c(' A', 'b ', ' C ', 'D')

# Generate pairwise comparisons within vec
ft_pairs(vec)

# Use a custom preprocessing function to convert factor_vec to lower case
ft_pairs(vec, pre_fn = function(x) tolower(trimws(x)))
ft_pairs(vec, ref = c("A","C"))

Run the code above in your browser using DataLab