I have this data:
names <- c("Baker, Chet", "Jarret, Keith", "Miles Davis")
I want to manipulate it so the first name come first, so i split it:
names <- strsplit(names, ", ")
[[1]]
[1] "Baker" "Chet"
[[2]]
[1] "Jarret" "Keith"
[[3]]
[1] "Miles Davis"
The problem is that, when i want to put them together, the name "Miles Davis"
will come out wrong, because it is already the full name
.
matrix(unlist(names), ncol=2, byrow = TRUE)
[,1] [,2]
[1,] "Baker" "Chet"
[2,] "Jarret" "Keith"
[3,] "Miles Davis" "Baker"
What should i do to create a new df
that will look like this:
"Chet Baker"
"Keith Jarret"
"Miles Davis"
Here's the reference: http://rfunction.com/archives/1499
You can easily adapt the pattern used in the regular expression so that it matches either a comma followed by 0+ spaces or 1+ spaces:
names <- strsplit(names, ",\\s*|\\s+")
matrix(unlist(names), ncol=2, byrow = TRUE)
# [,1] [,2]
#[1,] "Baker" "Chet"
#[2,] "Jarret" "Keith"
#[3,] "Miles" "Davis"
Since the desired result is different than initially described, heres's a different approach:
names <- strsplit(names, ",\\s*")
data.frame(name = sapply(names, function(x) paste(rev(x), collapse = " ")))
# name
#1 Chet Baker
#2 Keith Jarret
#3 Miles Davis
Another option, using capture groups in a regular expression to swap everything before the comma with everything after the comma and replace the comma with a space.
names <- c("Baker, Chet", "Jarret, Keith", "Miles Davis")
sub("([^,]+),\\s*([^,]+)$", "\\2 \\1", names)
#[1] "Chet Baker" "Keith Jarret" "Miles Davis"
Another regex solution:
gsub("(\\w+), (\\w+)", "\\2 \\1", names)
# [1] "Chet Baker" "Keith Jarret" "Miles Davis"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With