Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
439 views
in Technique[技术] by (71.8m points)

regex - clear Title from name using R

want to remove title from NameFirst column output will b like Clean_name column. any suggestions.

> df
           NAMEFIRST     Clean_name
1         BHASOTI MS        BHASOTI
2          BHABESHMR        BHABESH
3             RINAMS           RINA
4        SUSHMITAMRS       SUSHMITA
5         ARKADIY MR        ARKADIY
6  PRAMOD TRIMBAK DR PRAMOD TRIMBAK
7          ANDREW MR         ANDREW
8      MICHELLE MISS       MICHELLE
9         DINESHA MR        DINESHA
10        SREEDHARMR       SREEDHAR
11        PANKAJMSTR         PANKAJ
12   SUSHIL KUMAR MR   SUSHIL KUMAR
13          FAZLURMR         FAZLUR
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
df <- data.frame(name = c("RAMOREYDR","SAMUEL MR","MR KOOL","HANDSOMEDR","GELLER DR","SONIA MS"))
df
#         name
# 1  RAMOREYDR
# 2  SAMUEL MR
# 3    MR KOOL
# 4 HANDSOMEDR
# 5  GELLER DR
# 6   SONIA MS

df$Clean_Name <- gsub(" MR|MR|MR | MS|MS|MS | DR|DR|DR ", "", df$name)
df
#         name Clean_Name
# 1  RAMOREYDR    RAMOREY
# 2  SAMUEL MR     SAMUEL
# 3    MR KOOL       KOOL
# 4 HANDSOMEDR   HANDSOME
# 5  GELLER DR     GELLER
# 6   SONIA MS      SONIA

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...