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.

RownamesFirstMiddleLast
1John Jacob JingleNANANA
2Heimer SchmittNANANA
3CherNANANA
4John Jacob Jingle Heimer SchmittNANANA
5Mr. AndersonNANANA
6Sir Patrick StewartNANANA
7Sammy Davis Jr.NANANA

Replacing the first, middle, and last name values…

RownamesFirstMiddleLast
1John Jacob JingleJohnJacbonJingle
2Heimer SchmittHeimerNASchmitt
3CherCherNANA
4John Jacob Jingle Heimer SchmittJohnJacbon-Jingle-HeimerSchmitt
5Mr. AndersonNANAAnderson
6Sir Patrick StewartPatrickNAStewart
7Sammy Davis Jr.SammyNADavis

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.

V1V2V3
Matt SandyMattNASandy

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!

3 Comments

  1. Jamie Lendrum Jamie Lendrum October 22, 2019

    You may want to look at the humaniformat package.

  2. Gaby Gaby January 20, 2022

    Thank you for sharing this! Would there be a way to keep in the salutations and suffixes instead of getting rid of them? I am using this function to try and parse a list of names, but I would like to be able to have additional columns for the prefixes and suffixes. Thanks!

Leave a Reply

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