Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split names and create matrix in R

Tags:

r

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

like image 237
Lucca Ramalho Avatar asked May 16 '18 12:05

Lucca Ramalho


2 Answers

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" 
like image 75
talat Avatar answered Oct 21 '22 05:10

talat


Another regex solution:

gsub("(\\w+), (\\w+)", "\\2 \\1", names)
# [1] "Chet Baker"   "Keith Jarret" "Miles Davis" 
like image 3
sindri_baldur Avatar answered Oct 21 '22 07:10

sindri_baldur