5

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

m0nhawk
  • 22,980
  • 9
  • 45
  • 73
JB17
  • 65
  • 1
  • 8

4 Answers4

13
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.

JohannesNE
  • 1,343
  • 9
  • 14
7

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  
Heroka
  • 12,889
  • 1
  • 28
  • 38
4

One way using srt_split_fixed:

library(stringr)
#split Name into two columns
splits <- str_split_fixed(df$Name, ", ", 2)

#now merge these two columns the other way round
df$Name <- paste(splits[,2], splits[,1], sep = ' ')

Output:

           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  Ameer Abdullah  RB  det      1   2015  2995     a    sdg        19.4        4000      2   2015  2995
2   Davante Adams  WR  gnb      1   2015  5263     a    chi         9.9        4400      2   2015  5263
3  Nelson Agholor  WR  phi      1   2015  5378     a    atl         1.5        5700      2   2015  5378
4     Kamar Aiken  WR  bal      1   2015  5275     a    den         0.9        3300      2   2015  5275
5  Seyi Ajirotutu  WR  phi      1   2015  3877     a    atl         0.0        3000     NA     NA    NA
6    Dwayne Allen  TE  ind      1   2015  4551     a    buf        10.7        3400      2   2015  4551
LyzandeR
  • 37,047
  • 12
  • 77
  • 87
  • This works well, but there is a space before the first letter of each name. Added an extra step to clear this. df$Name = str_trim(df$Name) – JB17 Nov 20 '15 at 12:41
  • When I run your solution, I get a leading space at the start of each 'new' Name. Did you mean to split on `", "`? – Heroka Nov 20 '15 at 12:47
  • I guess `stri_extract_all` from `stringi` would be faster – akrun Nov 20 '15 at 13:04
  • Ahhh yes @Heroka thanks you are right. I should have split on `', '` – LyzandeR Nov 20 '15 at 13:49
  • @akrun Yeah thanks akrun you are right :) You could probably add it to be fair. `stri_extract_all` works in a different way than `str_split_fixed` – LyzandeR Nov 20 '15 at 13:49
  • @LyzandeR It's okay. You can add it to the mix and also update the benchmarks. – akrun Nov 20 '15 at 15:12
0

Try this one:

df$Name2<-paste(gsub("^.+\\,","",df$Name),gsub("\\,.+$","",df$Name),sep=" ")

where df is your data frame.

Roland
  • 127,288
  • 10
  • 191
  • 288