fast.string

Parallel string, date & phonetic-matching functions for R

Author

Kyle Haynes

Published

13 November 2026

Preface

fast.string is an R package for the string, date and name-matching work that record linkage and ETL pipelines spend their time on. It has three layers:

  • rLink(), which links or deduplicates data frames in one call: multi-pass blocking, any number of comparators, one table of scored candidate pairs (sec-uc-link).
  • The record-linkage primitives base R does not have — phonetic blocking keys (Soundex, NYSIIS, Double Metaphone, Daitch-Mokotoff, Caverphone, …), string similarities and edit distances (Jaro-Winkler, Levenshtein, Editex, q-gram overlap, the RapidFuzz scorers), candidate search without a comparison matrix, and connected-component clustering.
  • Faster equivalents of the everyday functions — grepl(), grep(), sub(), gsub(), trimws(), substr(), nchar(), chartr(), strsplit(), toupper(), date parsing and formatting — so the cleaning either side of a linkage is not the slow part.

Large workloads are parallelised via Intel TBB (through RcppParallel), with PCRE2 doing the regex work and a prepared byte-oriented searcher handling fixed/literal matching. Performance depends on string length, match density, value reuse, matrix shape, and available cores. Some operations (parallel Jaro-Winkler, multi-pattern substitution in one pass) have no base R equivalent at all.

Why this exists

Record linkage and data-cleaning pipelines spend a surprising fraction of their wall-clock time in string operations that look trivial in isolation — trimws() on a column, a gsub() to strip punctuation, grepl() to flag a pattern — but get called over and over on multi-million-row name/address files. Individually each call is fast; in aggregate, on a 5M-row file, they add up to minutes of CPU time spent doing work a single core could be doing across eight or sixteen instead.

Four things are conspicuously not one function each in base R, despite being everywhere in this kind of pipeline:

  • Phonetic blocking keys (Soundex, NYSIIS, Refined Soundex, Cologne, Double Metaphone, Caverphone) — used to group probably-same names before running an expensive pairwise comparison within each group, rather than comparing every record against every other record.
  • Fuzzy string similarity (Jaro-Winkler, edit distance, q-gram overlap) — base R has no notion of “how similar are these two strings,” only exact (==) or pattern (grepl()) matching.
  • Multi-token name comparison — "Kyle John Haynes" and "John Kylie Haynes" are obviously the same person with two tokens swapped, but a plain character-by-character comparison scores that badly.
  • Per-element match counting — grepl() answers “is there a match”; counting how many means gregexpr() plus a vapply() that allocates a position vector per row only to take its length.

fast.string exists to make all of the above fast enough to run unblinkingly on real-sized data, and to package the boring-but-essential 80% (trimming, substitution, date parsing) so a project doesn’t need five different dependencies for five different speed problems.

How it fits with record-linkage packages

rLink() covers one step of a linkage: two files in, scored candidate pairs out. It does not classify them. Estimating match probabilities from the scores — EM-fitted Fellegi-Sunter weights, training against a labelled sample — is what RecordLinkage, reclin2 and fastLink are for, and they do it well. The natural division is rLink() for the blocking and the per-field comparison, where the string work and the parallelism are, and one of those packages for the model that decides. sec-benchmarks compares this package’s comparison primitives directly against the equivalent functions in RecordLinkage and stringdist at scale, and sec-wf-score shows how far hand weights get you and where they stop.

How this book is organised

  • Getting started — what fast.string is for and how it’s positioned against base R, stringi/stringr, stringdist, and RecordLinkage (sec-about), then a hands-on tour of every function group (sec-quickstart).
  • Use cases — task-oriented recipes: cleaning and standardising text (entities, accents, abbreviation dictionaries, leading zeros, spelling variants; sec-uc-clean), finding, flagging and extracting values from free text (sec-uc-extract), profiling and validating columns (format shapes, identifier check digits; sec-uc-profile), matching, deduplicating and linking records at scale with rLink() and the primitives under it (sec-uc-link), and pseudonymising identifiers with keyed hashes (sec-uc-privacy).
  • Under the hood — the algorithms (sec-theory): how regex and fixed-literal matching differ, how the bit-parallel Jaro-Winkler implementation works, how the phonetic schemes trade block size against recall, and how multi-token name comparison resolves reordered tokens — followed by a full worked record-linkage pipeline (sec-workflow) that links two files of people whose names, dates of birth, addresses, sex and postcodes are each recorded differently: profiling, standardising field by field, multi-pass blocking, per-field scoring, a defensible accept/review/reject decision, entity identifiers, and a measurement of how well it worked against a known truth.
  • Reference — an exhaustive per-function reference with every argument and runnable examples (sec-reference); the heavy benchmark chapter at 3 million strings, with base, stringi, stringdist, and RecordLinkage as comparison baselines (sec-benchmarks); and a troubleshooting/FAQ chapter (sec-troubleshooting).

A quick taste

A whole linkage — blocking, scoring, one row per candidate pair — is one call. Two small files of people, with a surname spelled two ways, a nickname instead of a registered given name, and a date of birth whose day and month were transposed:

enrol <- data.frame(
    id      = c("E1", "E2", "E3", "E4"),
    given   = c("ROBERT", "ELIZABETH", "MOHAMMED", "LI"),
    surname = c("O'BRIEN", "SMITH-JONES", "EL-MASRI", "NGUYEN"),
    dob     = as.Date(c("1985-03-05", "1972-11-30", "1990-07-14", "1964-02-29")),
    sex     = c("M", "F", "M", "F"))

claims <- data.frame(
    id      = c("C1", "C2", "C3"),
    given   = c("BOB", "LIZ", "MOHAMED"),
    surname = c("OBRIEN", "SMITH JONES", "EL MASRI"),
    dob     = as.Date(c("1985-05-03", "1972-11-30", "1990-07-14")),
    sex     = c("M", "F", "M"))

rLink(enrol, claims,
      block   = list(exact_dob    = list(dob),
                     surname_year = list(soundex(surname),
                                         format_date(dob, "y"))),
      compare = list(sn  = jaro_winkler_tokens(surname),
                     gn  = jaro_winkler(given),
                     dob = (function(a, b) as.numeric(a == b))(dob)),
      keep    = c("id", "given", "surname", "dob"))
   pair_id index_1 index_2  pass  block_key   id_1   id_2   given_1 given_2
     <int>   <int>   <int> <int>     <char> <char> <char>    <char>  <char>
1:       1       1       1     2  O165|1985     E1     C1    ROBERT     BOB
2:       2       2       2     1 1972-11-30     E2     C2 ELIZABETH     LIZ
3:       3       3       3     1 1990-07-14     E3     C3  MOHAMMED MOHAMED
          gn   surname_1   surname_2    sn      dob_1      dob_2   dob
       <num>      <char>      <char> <num>     <Date>     <Date> <num>
1: 0.5000000     O'BRIEN      OBRIEN     1 1985-03-05 1985-05-03     0
2: 0.7777778 SMITH-JONES SMITH JONES     1 1972-11-30 1972-11-30     1
3: 0.9750000    EL-MASRI    EL MASRI     1 1990-07-14 1990-07-14     1

All three claims found their person, and the pass column says how: two on the exact date of birth, and Robert O’Brien — whose day and month had been swapped — on the second pass, a phonetic surname code plus the year alone. jaro_winkler_tokens() scores "SMITH-JONES" against "SMITH JONES" at 1, because it folds punctuation and word order inside the comparison. sec-uc-link and sec-workflow take this all the way through on data that is properly messy.

And the ordinary string work underneath it, on a million values:

n <- 1e6
nm  <- sprintf("%s %s", sample(c("John", "Mary", "Kyle", "Priya"), n, TRUE),
               sample(c("Smith", "O'Brien", "Haynes", "Nguyen"), n, TRUE))
dob <- as.Date("1950-01-01") + sample(0:25000, n, TRUE)

rbind(toupper     = system.time(toupper(nm))[1:3],
      fupper      = system.time(fupper(nm))[1:3],
      format      = system.time(format(dob, "%Y-%m-%d"))[1:3],
      format_date = system.time(format_date(dob))[1:3])
            user.self sys.self elapsed
toupper          0.48     0.00    0.48
fupper           0.16     0.11    0.04
format           5.36     0.00    5.36
format_date      0.09     0.17    0.05

format_date() is the extreme case: format() on a Date reaches strftime() once per element, and replacing that with integer calendar arithmetic is worth two orders of magnitude. Most functions here are nearer 2—10x, a few are no faster than base R at all (sec-troubleshooting says which), and the phonetic codes and similarity scores have no base R equivalent to be compared with.