Separate "Name" into "FirstName" and "LastName" columns of data frame

seperate or separate
separate synonyms
separate meaning
put separately
separated
separated synonym
separate in hindi
a word for separate

I am struggling to figure out how to take a single column of "Name" in a dataframe split it into two other columns of FistName and LastName within the same data frame. The challenge is that some of my Names have several last names. Essentially, I want to take the first word (or element of the string) and put it in the FirstName columns, then put all following text (minus the space of course) into the LastName column.

This is my DataFrame "tteam"

NAME <- c('John Doe','Peter Gynn','Jolie Hope-Douglas', 'Muhammad Arnab Halwai')
TITLE <- c("assistant", "manager", "assistant", "specialist")
tteam<- data.frame(NAME, TITLE)

My desired output would like this:

FirstName <- c("John", "Peter", "Jolie", "Muhammad")
LastName <- c("Doe", "Gynn", "Hope-Douglas", "Arnab Halwai")
tteamdesire <- data.frame(FirstName, LastName, TITLE)

I have tried the following code to create a new data frame of just names that allow me to extract the first names from the first column. However, I am unable to put the last names into any order.

names <- tteam$NAME ##  puts full names into names vector
namesdf <- data.frame(do.call('rbind', strsplit(as.character(names),' ',fixed=TRUE))) 
## splits out all names into a dataframe PROBLEM IS HERE!

You could use extract from tidyr

 library(tidyr)
 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

Separate, synonyms: unconnected, unrelated, different, discrete, distinct, disparate, detached, divorced, disconnected, independent, autonomous, respective, individual, particular, several Separate definition, to keep apart or divide, as by an intervening barrier or space: to separate two fields by a fence. See more.


Try:

> firstname = sapply(strsplit(NAME, ' '), function(x) x[1])
> firstname 
[1] "John"     "Peter"    "Jolie"    "Muhammad"

> lastname = sapply(strsplit(NAME, ' '), function(x) x[length(x)])
> lastname
[1] "Doe"          "Gynn"         "Hope-Douglas" "Halwai"      

or:

> ll = strsplit(NAME, ' ')
> 
> firstname = sapply(ll, function(x) x[1])
> lastname = sapply(ll, function(x) x[length(x)])
> 
> firstname
[1] "John"     "Peter"    "Jolie"    "Muhammad"
> lastname
[1] "Doe"          "Gynn"         "Hope-Douglas" "Halwai"      

Separate Synonyms, Separate Antonyms, synonyms: part, split (up), break up, move apart, divide, sunder Existing or considered as an independent entity: The reference collection is separate from the rest of the library.


1) sub

data.frame(FirstName = sub(" .*", "", tteam$NAME), 
           LastName = sub("^\\S* ", "", tteam$NAME),
           tteam[-1])

2) gsubfn::read.pattern In the NAME<- we can omit as.character if its already character (as opposed to factor):

library(tteam)

cn <- c("FirstName", "LastName")
NAME <- as.character(tteam$NAME)

cbind( read.pattern(text = NAME, pattern = "^(\\S*) (.*)", col.names = cn), tteam[-1])

Update Update solution to be in terms of tteam and add second solution.

SEPARATE, divide or cause to divide into constituent or distinct elements. existing or happening independently or in a different physical space: The art department and the music department are in two separate buildings. I try to keep meat separate from other food in the refrigerator. I have my public life and my private life, and as far as possible I try to keep them separate.


You could use the package unglue :

library(unglue)
unglue_unnest(tteam, NAME, "{FirstName} {LastName}")
#>        TITLE FirstName     LastName
#> 1  assistant      John          Doe
#> 2    manager     Peter         Gynn
#> 3  assistant     Jolie Hope-Douglas
#> 4 specialist  Muhammad Arnab Halwai

Separate vs. Seperate–Which Should I Use?, make something form, or view something as, a unit apart or by itself. In a seperate incident, a man was shot and injured by muggers over resistance in Orangi Town.According to rescue sources, two robbers riding a motorcycle tried to loot a 25-year-old man but he offered them resistance. 2 men injured in seperate incidents


Separate, synonyms: isolate, set apart, put to one side, segregate, sort out, sift out, winnow out, filter out, remove, weed out, distinguish, differentiate, dissociate 90 synonyms of separate from the Merriam-Webster Thesaurus, plus 157 related words, definitions, and antonyms. Find another word for separate. Separate: not the same or shared.


Separate, individual items of clothing, such as skirts, jackets, or pants, suitable for wearing in different combinations. Separate can be an adjective or a verb. As an adjective, it means set apart, distinct, or not related. As a verb, it means to to set apart, to distinguish, or to divide. Separate is often misspelled as seperate, a word that has no meaning and is simply a misspelling:


separate, portions into which a soil, sediment, etc. can be sorted according to particle size, mineral composition, or other criteria. Separately definition, to keep apart or divide, as by an intervening barrier or space: to separate two fields by a fence. See more.