Skip to contents

Invert 'LastName FirstName' to 'FirstName LastName' (or the reverse)

Usage

invert_names(names, correct_names)

Arguments

names

A character vector of potentially inverted names

correct_names

A character vector of correct names

Value

A character vector with the same length as names

Details

When you have a list x of mixed 'First Last' and 'Last First' names, but no source of truth and you want to deduplicate them, this function can be used as expand_names(x, x), which will return the most common version available in x for each name.

Examples

invert_names(
  c("Wolfgang Mozart", "Mozart Wolfgang"),
  "Wolfgang Mozart"
)
#> [1] "Wolfgang Mozart" "Wolfgang Mozart"

# Real-case application example
# Deduplicate names in list, as described in "details"
epi_pkg_authors <- cran_epidemiology_packages |>
  subset(!is.na(`Authors@R`), `Authors@R`, drop = TRUE) |>
  parse_authors_r() |>
  # Drop email, role, ORCID and format as string rather than person object
  lapply(function(x) format(x, include = c("given", "family"))) |>
  unlist()

# With all duplicates
length(unique(epi_pkg_authors))
#> [1] 367

# Deduplicate
epi_pkg_authors_normalized <- invert_names(epi_pkg_authors, epi_pkg_authors)

length(unique(epi_pkg_authors_normalized))
#> [1] 367