Press "Enter" to skip to content

Split Intermixed Names into First, Middle, and Last

Data cleaning can be a challenge, so I hope this helps the process for someone out there. This is a tiny, but valuable function for those who deal with data collected from non-ideal forms. As nearly always, this depends on the tidyverse library. You may want to rename the function from fml, but it does best describe dealing with mangled data.

This function retuns the first, middle, and last names for a given name or list of names. Missing data is represented as NA.

Usage on Existing Dataframe

Setting up a dataframe with manged names and missing first, middle, and last names.

Row names First Middle Last
1 John Jacob Jingle NA NA NA
2 Heimer Schmitt NA NA NA
3 Cher NA NA NA
4 John Jacob Jingle Heimer Schmitt NA NA NA
5 Mr. Anderson NA NA NA
6 Sir Patrick Stewart NA NA NA
7 Sammy Davis Jr. NA NA NA

Replacing the first, middle, and last name values…

Row names First Middle Last
1 John Jacob Jingle John Jacbon Jingle
2 Heimer Schmitt Heimer NA Schmitt
3 Cher Cher NA NA
4 John Jacob Jingle Heimer Schmitt John Jacbon-Jingle-Heimer Schmitt
5 Mr. Anderson NA NA Anderson
6 Sir Patrick Stewart Patrick NA Stewart
7 Sammy Davis Jr. Sammy NA Davis

Values Changed

  • In row 1 All names were found
  • In row 2 the middle name was skipped
  • In row 3 only a first name was found
  • In row 4 the middle names were collapsed
  • In row 5 only a last name was found
  • In row 6 the title Sir was omitted
  • In row 7 the title Jr. was omitted

Using with a single name.

V1 V2 V3
Matt Sandy Matt NA Sandy

The Function

Improvements

I recommend improving upon this if you want to integrate this function (or attributes of this function) into your workflow. Naming the output or using lists so you can just get partial returns fml("John Smith")$Last could come in handy.

Additional cases could also be created, such as when names are entered Last, First M.. Tailoring the function to your project will yield best results.

Sharing is caring!

2 Comments

  1. Jamie Lendrum Jamie Lendrum October 22, 2019

    You may want to look at the humaniformat package.

Leave a Reply to Bob Muenchen Cancel reply

Your email address will not be published. Required fields are marked *