I am trying to figure out how to take one column “Name” in a data framework, split it into two other columns FistName and LastName in the same data frame. The problem is that some of my names have several last names. Essentially, I want to take the first word (or row element) and put it in the FirstName columns, and then put all the next text (minus the space, of course) in 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 result:
FirstName <- c("John", "Peter", "Jolie", "Muhammad")
LastName <- c("Doe", "Gynn", "Hope-Douglas", "Arnab Halwai")
tteamdesire <- data.frame(FirstName, LastName, TITLE)
I tried the following code to create a new data frame only for names that allow me to extract the first names from the first column. However, I cannot put the last names in any order.
names <- tteam$NAME
namesdf <- data.frame(do.call('rbind', strsplit(as.character(names),' ',fixed=TRUE)))