Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

strsplit

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!
like image 281
RyanL Avatar asked Oct 21 '14 14:10

RyanL


People also ask

How do you split first and last name into two columns in SQL?

How do I separate first name and last name in SQL? Using SUBSTRING Function The CHARINDEX function returns the starting position of a specified expression in a character string. The CHARINDEX function was used to look for the space that separates the first name from the last name (CHARINDEX(' ', @FullName)).

How do I separate 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.

How do I split a column into a list in R?

Method 1: Using str_split_fixed() function of stringr package library. To split a column into multiple columns in the R Language, We use the str_split_fixed() function of the stringr package library. The str_split_fixed() function splits up a string into a fixed number of pieces.


2 Answers

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
like image 144
akrun Avatar answered Sep 19 '22 21:09

akrun


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"      
like image 38
rnso Avatar answered Sep 20 '22 21:09

rnso