简体   繁体   中英

R paste after strsplit without duplicates

I have a string in R

myname <- "Peter del Soreno Garfias"

I want to get rid of the second word and would like to have "Peter Soreno Garfias" in the end.

So I do a

strgSplitted <- unlist(strsplit(myname))

And put everything together

nameShort <- paste0(strgSplitted[[1]]," ",strgSplitted[1:length(strgSplitted)]

But what I get is

"Peter Soreno, Peter Garfias"

How can I get

"Peter Soreno Garfias"

?

Best

You could use strsplit and remove the 2nd word

paste(unlist(strsplit(myname, " "))[-2], collapse = " ")
[1] "Peter Soreno Garfias"

If it is a specific word and not necessarily at the 2nd position, then you can replace that with blank:

sub("del", "", myname)
[1] "Peter  Soreno Garfias"

Or only keep words with Capital letters

library(stringr)
paste(str_extract_all(myname, "\\b[A-Z]\\w+")[[1]], collapse = " ")
[1] "Peter Soreno Garfias"

Here is a possibly safer approach using gsub :

myname <- "Peter del Soreno Garfias"
output <- gsub("\\s+[a-z]+\\s+", " ", myname)
output

[1] "Peter Soreno Garfias"

The logic here is to remove any middle words which do not have any capital letters in them.

If you really just want to remove the second word from the name, we can also use sub for that:

sub("^(\\S+)\\s+\\S+(.*)", "\\1\\2", myname)
[1] "Peter Soreno Garfias"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM