idf4floor
idf4floor

Reputation: 73

Split a column of full name in to a column of first and last name when it's separated by space in R

I have a data frame with one of the columns consisting of a full_name, instead of this I would like to have a column of firs_name and last_name.

For example: say my data is called datta

head(datta)

    full_name       V1    V2
    lee yees        4     4
    jhon morgan     4     5
    lebron tom      5     5

I would like to get:

head(datta)       
first_name  last_name  V1  V2
lee         yees       4   4
jhon        morgan     4   5
lebron      tom        5   5

I saw a similar question here in the link

Efficient way to split a vector of a full name in to 2 separate vectors

The only difference is that in that question the names were separated by a comma and in my data it is separated by space. I tried to apply what was shown on the answers just using space instead of a comma.

lst <- strsplit(val.vec,' ')
v1 <- lapply(lst, `[`, 1)
v2 <- lapply(lst, `[`, 2)

It didn't work; it returned a list where each element is one object the first and last name.

Upvotes: 0

Views: 1505

Answers (2)

Adarsh N
Adarsh N

Reputation: 11

Using dplyr and stringr

datta %>% mutate(first_name = word(full_name,1),last_name = word(full_name,2))

Upvotes: 1

akrun
akrun

Reputation: 887611

We can use separate

library(tidyr)
separate(datta, full_name, into = c("first_name", "last_name"))
#   first_name last_name V1 V2
#1        lee      yees  4  4
#2       jhon    morgan  4  5
#3     lebron       tom  5  5

Or using read.table from base R

cbind(read.table(text=datta$full_name, header=FALSE, 
           col.names = c("first_name", "last_name")), datta[-1])

Upvotes: 2

Related Questions