Reputation: 65
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
Upvotes: 5
Views: 8783
Reputation: 1363
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.
Upvotes: 13
Reputation: 180
Try this one:
df$Name2<-paste(gsub("^.+\\,","",df$Name),gsub("\\,.+$","",df$Name),sep=" ")
where df
is your data frame.
Upvotes: 0
Reputation: 37889
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
Upvotes: 4
Reputation: 13149
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
Upvotes: 7