Warning: package 'dplyr' was built under R version 4.2.3
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
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).
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
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://ncraig.netlify.app/posts/2022-10-21-invert-names/index.html},
langid = {en}
}