Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Last name, First Name to First Name Last Name

I have a set of names in last, first format

             Name Pos Team Week.x Year.x GID.x h.a.x Oppt.x Week1Points DK.salary.x Week.y Year.y GID.y
1 Abdullah, Ameer  RB  det      1   2015  2995     a    sdg        19.4        4000      2   2015  2995
2  Adams, Davante  WR  gnb      1   2015  5263     a    chi         9.9        4400      2   2015  5263
3 Agholor, Nelson  WR  phi      1   2015  5378     a    atl         1.5        5700      2   2015  5378
4    Aiken, Kamar  WR  bal      1   2015  5275     a    den         0.9        3300      2   2015  5275
5 Ajirotutu, Seyi  WR  phi      1   2015  3877     a    atl         0.0        3000     NA     NA    NA
6   Allen, Dwayne  TE  ind      1   2015  4551     a    buf        10.7        3400      2   2015  4551

That is just the fist 6 lines. I would like to flip the names to First name Last Name. Here is what I tried.

> strsplit(DKPoints$Name, split = ",")

This splits the name variable, but there are white spaces, so to clear them I tried,

> str_trim(splitnames)

But the results did not come out right. Here is what they look like.

  [1] "c(\"Abdullah\", \" Ameer\")"          "c(\"Adams\", \" Davante\")"          
  [3] "c(\"Agholor\", \" Nelson\")"          "c(\"Aiken\", \" Kamar\")"            
  [5] "c(\"Ajirotutu\", \" Seyi\")"          "c(\"Allen\", \" Dwayne\")"

Any advice? I would like to get a column for the data frame to look like

Ameer Abdullah
Davabte Adams
Nelson Agholor
Kamar Aiken

Any advice would be much appreciated. Thanks

like image 551
JB17 Avatar asked Nov 20 '15 12:11

JB17


People also ask

What is the order of first name and last name?

Generally, the name of an individual is broken down into two halves. The first name is the name given at birth (Sachin). The last name (surname) represents the name of the family to which the child is born (Tendulkar).


2 Answers

sub("(\\w+),\\s(\\w+)","\\2 \\1", df$name)

(\\w+) matches the names, ,\\s matches ", "(comma and space), \\2 \\1 returns the names in opposite order.

like image 199
JohannesNE Avatar answered Sep 18 '22 15:09

JohannesNE


Assuming all names are "Lastname, firstname" you could do something like this:

names <- c("A, B","C, D","E, F")


newnames <- sapply(strsplit(names, split=", "),function(x) 
  {paste(rev(x),collapse=" ")})

> newnames
[1] "B A" "D C" "F E"

It splits each name on ", " and then pastes things back together in reverse order.

Edit: probably no problem for small datasets, but the other solutions provided are a lot faster. Microbenchmark results for 100.000 'names':

Unit: milliseconds
     expr       min        lq      mean    median        uq       max neval cld
   heroka 1103.0419 1242.6418 1276.7765 1274.6746 1311.1218 1557.8579    50   c
 lyzander  149.4466  177.0036  206.4558  191.1249  218.1756  345.7960    50  b 
 johannes  142.7585  144.5943  151.0078  146.0602  147.1980  284.2589    50 a  
like image 38
Heroka Avatar answered Sep 17 '22 15:09

Heroka