fuzzyjoin R Package

repository·master·Indexed 20 days ago

https://github.com/dgrtwo/fuzzyjoin

An R package that extends dplyr join operations to support inexact matching. It provides tools for joining data frames based on string similarity, numeric tolerance, regular expressions, geographic distance, interval overlaps, and genomic intervals. It supports all six dplyr join operations and includes a general fuzzy_join wrapper for custom matching functions.

Tokens
800
Snippets
3
Records
5
Agent score
22%

What's inside fuzzyjoin

  1. Overview of fuzzyjoin matching types

    master

    The fuzzyjoin package provides variations of dplyr join operations that allow matching based on inexact criteria rather than exact equality. Supported matching types include:

    • Numeric tolerance: Matches numeric values within a specified tolerance using difference_inner_join.
    • String similarity: Matches strings based on Levenshtein, cosine, Jaccard, or other stringdist metrics using stringdist_inner_join.
    • Regular expressions: Matches a regular expression in one column to a value in another using regex_inner_join.
    • Distance metrics: Matches across multiple columns using Euclidean or Manhattan distance via distance_inner_join.
    • Geographic distance: Matches based on longitude and latitude using geo_inner_join.
    • Interval overlaps: Matches overlapping (start, end) intervals using interval_inner_join.
    • Genomic intervals: Matches overlapping genomic intervals (chromosome ID + start/end pairs) using genome_inner_join.

    Each matching type supports all six dplyr join operations (e.g., inner_join, left_join, right_join, full_join, semi_join, and anti_join).

  2. Install the fuzzyjoin package

    master

    You can install fuzzyjoin from CRAN or the development version from GitHub.

    From CRAN:

    install.packages("fuzzyjoin")

    From GitHub (requires devtools):

    devtools::install_github("dgrtwo/fuzzyjoin")
    install.packages("fuzzyjoin")
  3. Use regex_inner_join for pattern-based classification

    master

    Use regex_inner_join to join a data frame of text against a data frame of regular expressions. This allows you to classify or tag text based on whether specific patterns are present.

    Important Note: The data frame containing the text to be searched must be the first argument in the join.

    Example:

    library(dplyr)
    library(fuzzyjoin)
    
    # Classify passages based on character name regexes
    character_passages <- passages %>%
      regex_inner_join(characters, by = c(text = "character_regex"))
    character_passages <- passages %>%
      regex_inner_join(characters, by = c(text = "character_regex"))
  4. Use stringdist_inner_join for string similarity matching

    master

    Use stringdist_inner_join to join two data frames based on string similarity. This is useful for correcting misspellings against a dictionary or classifying freeform text.

    Key Arguments:

    • by: A named vector specifying the columns to join (e.g., c(left_col = "right_col")).
    • max_dist: The maximum allowable distance for a match.
    • distance_col: (Optional) The name of a new column to store the calculated distance for each match.

    Example:

    library(dplyr)
    library(fuzzyjoin)
    
    # Join sub_misspellings to words where distance is at most 1
    joined <- sub_misspellings %>%
      stringdist_inner_join(words, by = c(misspelling = "word"), max_dist = 1)
    
    # Join and include the distance in the output
    joined_dists <- sub_misspellings %>%
      stringdist_inner_join(words, by = c(misspelling = "word"), max_dist = 2,
                            distance_col = "distance")
    joined <- sub_misspellings %>%
      stringdist_inner_join(words, by = c(misspelling = "word"), max_dist = 1)