This question is an extension of another post titled - Separate “Name” into “FirstName” and “LastName” columns of data frame - and the data frame is provided below.
NAME <- c('John Doe','Peter Gynn','Jolie Hope-Douglas', 'Muhammad Arnab Halwai')
TITLE <- c("assistant", "manager", "assistant", "specialist")
tteam<- data.frame(NAME, TITLE)
A nice code was provided to show how to use tidyr
to separate each name into first and last name. I have a situation where there are names with three parts, such as the name provided in the example - "Muhammad Arnab Halwai". I believe the code below will separate the name into "Muhammad" and "Arnab Halwai". I'm trying to reconfigure the code to create "Muhammad Arnab" and "Halwai".
library(tidyr)
extract(tteam, NAME, c("FirstName", "LastName"), "([^ ]+) (.*)")
Just got to switch up the regex a bit:
extract(tteam, NAME, c("FirstName", "LastName"), "(.*) ([^ ]+)$")
# FirstName LastName TITLE
# 1 John Doe assistant
# 2 Peter Gynn manager
# 3 Jolie Hope-Douglas assistant
# 4 Muhammad Arnab Halwai specialist
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.