Search code examples
rstringsplit

Splitting and swapping names


I have names in the following format;

name1
"brown-john"
name2
"bloggs-joe"

To which i want to now change to read john brown and joe bloggs. Removing the '-' they are separated by isnt too much if an issue but im struggling to flip the names if anyone knows how thats possible?


Solution

  • You can use gsub:

    name <- c(name1 = "brown-john", name2 = "bloggs-joe")
    gsub("(.*)-(.*)", "\\2 \\1", name)
    #       name1        name2 
    #"john brown" "joe bloggs"