# Load libraries
library(dplyr)
Warning: package 'dplyr' was built under R version 4.2.3
library(tidyr)
Warning: package 'tidyr' was built under R version 4.2.3
A short note on switching inverted names like last, first
to read first last
.
Nathan Craig
October 21, 2022
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).
Warning: package 'dplyr' was built under R version 4.2.3
Warning: package 'tidyr' was built under R version 4.2.3
df <- df |> separate(name_inverse, c("last", "first"),
remove = FALSE) |>
mutate(name = paste(first, last, sep = " "),
.before = name_inverse)
df
select
.
name | name_inverse | last | first |
---|---|---|---|
John Henry | Henry, John | Henry | John |
Paul Bunyan | Bunyan, Paul | Bunyan | Paul |
@online{craig2022,
author = {Craig, Nathan},
title = {Invert {Names}},
date = {2022-10-21},
url = {https://nmc.quarto.pub/nmc/posts/2022-10-21-invert-names},
langid = {en}
}