Invert Names

A short note on switching inverted names like last, first to read first last.

how-to
wrangling
museum
Author

Nathan Craig

Published

October 21, 2022

Modified

March 21, 2024

Occasionally, we get data frames where items are comma separated and reverse order. Perhaps the most common case is where first and last names are reversed and comma separated. Another, situation is when applying controlled vocabularies, which are often structured in this way (Figure 1). Let’s run through switching the terms and see the result (#tbl-result).

Figure 1: PastPerfect data entry screen showing assignment of Nomenclature 3.0 terms.
# Load libraries
library(dplyr)
Warning: package 'dplyr' was built under R version 4.2.3
Warning: package 'tidyr' was built under R version 4.2.3
# Create data
df <- data.frame (name_inverse  = c("Henry, John", 
                      "Bunyan, Paul")
                  )
df <- df |> separate(name_inverse, c("last", "first"),
               remove = FALSE) |> 
  mutate(name = paste(first, last, sep = " "), 
         .before = name_inverse)
df
Table 1: Results showing the reversal of names. Note the extra columns can be removed with select.
name name_inverse last first
John Henry Henry, John Henry John
Paul Bunyan Bunyan, Paul Bunyan Paul
Figure 2: Names fixed

Citation

BibTeX citation:
@online{craig2022,
  author = {Craig, Nathan},
  title = {Invert {Names}},
  date = {2022-10-21},
  url = {https://ncraig.netlify.app/posts/2022-10-21-invert-names/index.html},
  langid = {en}
}
For attribution, please cite this work as:
Craig, Nathan. 2022. “Invert Names.” October 21, 2022. https://ncraig.netlify.app/posts/2022-10-21-invert-names/index.html.