How to create separate columns for first and last name in R?


Most of the times in data analysis, the first name and last name of people are joined together or we can say they are stored in a single space hence we need to separate them to make the data easily readable. To create separate columns for first and last name in R, we can use extract function of tidyr package.

Check out the below given examples to understand how it can be done.

Example 1

Following snippet creates a sample data frame −

Names<-c("John Jones","Steve Smith","Pat Cummins","David Warner","Andrew Flintoff","Aaron Finch","Mitchell Starc","Nathan Lyon","Mathew Wade","Adam Zampa","Adam Gilchrist","Ricky Ponting","Glenn McGrath","Ben Cutting","John Cena","Brock Williams","Rubel Hussain","Soumya Sarkar","Mehidy Hasan","Liton Das")
df1<-data.frame(Names)
df1

The following dataframe is created −

    Names
1  John Jones
2  Steve Smith
3  Pat Cummins
4  David Warner
5  Andrew Flintoff
6  Aaron Finch
7  Mitchell Starc
8  Nathan Lyon
9  Mathew Wade
10 Adam Zampa
11 Adam Gilchrist
12 Ricky Ponting
13 Glenn McGrath
14 Ben Cutting
15 John Cena
16 Brock Williams
17 Rubel Hussain
18 Soumya Sarkar
19 Mehidy Hasan
20 Liton Das

To load tidyr package and create separate column for first name and last name in df1, add the following code to the above snippet −

library(tidyr)
extract(df1,Names,c("First_Name","Last_Name"), "([^ ]+) (.*)")

Output

If you execute all the above given snippets as a single program, it generates the following output −

  First_Name Last_Name
1  John      Jones
2  Steve     Smith
3  Pat       Cummins
4  David     Warner
5  Andrew    Flintoff
6  Aaron     Finch
7  Mitchell  Starc
8  Nathan    Lyon
9  Mathew    Wade
10 Adam      Zampa
11 Adam      Gilchrist
12 Ricky     Ponting
13 Glenn     McGrath
14 Ben       Cutting
15 John      Cena
16 Brock     Williams
17 Rubel     Hussain
18 Soumya    Sarkar
19 Mehidy    Hasan
20 Liton     Das

Example 2

Following snippet creates a sample data frame −

Names<-c("Kane Williamson","Devon Conway","Trent Boult","Ross Taylor","Martin Guptill","Tim Southee","James Neesham","Lockie Ferguson","Ish Sodhi","Matt Henry","Tom Latham","Mark Chapman","Henry Nicholos","Tom Bundell","Sachin Tendulkar","Rahul Dravid","Chris Gayle","Tabraiz Shamsi","Aiden Makram","David Miller")
df2<-data.frame(Names)
df2

The following dataframe is created −

    Names
1  Kane Williamson
2  Devon Conway
3  Trent Boult
4  Ross Taylor
5  Martin Guptill
6  Tim Southee
7  James Neesham
8  Lockie Ferguson
9  Ish Sodhi
10 Matt Henry
11 Tom Latham
12 Mark Chapman
13 Henry Nicholos
14 Tom Bundell
15 Sachin Tendulkar
16 Rahul Dravid
17 Chris Gayle
18 Tabraiz Shamsi
19 Aiden Makram
20 David Miller

To create separate column for first name and last name in df2, add the following code to the above snippet −

extract(df2,Names,c("First_Name","Last_Name"), "([^ ]+) (.*)")

Output

If you execute all the above given snippets as a single program, it generates the following output −

 First_Name Last_Name
1  Kane     Williamson
2  Devon    Conway
3  Trent    Boult
4  Ross     Taylor
5  Martin   Guptill
6  Tim      Southee
7  James    Neesham
8  Lockie   Ferguson
9  Ish      Sodhi
10 Matt     Henry
11 Tom      Latham
12 Mark     Chapman
13 Henry    Nicholos
14 Tom      Bundell
15 Sachin   Tendulkar
16 Rahul    Dravid
17 Chris    Gayle
18 Tabraiz  Shamsi
19 Aiden    Makram
20 David    Miller

Updated on: 11-Nov-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements