如何搜索多个字符串并在字符串列表中不使用任何东西替换它们

时间:2021-06-18 16:49:12

I have a column in a dataframe like this:

我在dataframe中有一个这样的专栏:

npt2$name
#  [1] "Andreas Groll, M.D."
#  [2] ""
#  [3] "Pan-Chyr Yang, PHD"
#  [4] "Suh-Fang Jeng, Sc.D"
#  [5] "Mostafa K Mohamed Fontanet Arnaud"
#  [6] "Thomas Jozefiak, M.D."
#  [7] "Medical Monitor"
#  [8] "Qi Zhu, MD"
#  [9] "Holly Posner"
# [10] "Peter S Sebel, MB BS, PhD Chantal Kerssens, PhD"
# [11] "Lance A Mynderse, M.D."
# [12] "Lawrence Currie, MD"

I tried gsub but with no luck. After doing toupper(x) I need to replace all instances of 'MD' or 'M.D.' or 'PHD' with nothing.

我试过潜水服,但运气不好。做了toupper(x)之后,我需要替换所有的“MD”或“M.D.”实例或者什么都没有的博士学位。

Is there a nice short trick to do it?

有什么简单的方法吗?

In fact I would be interested to see it done on a single string and how differently it is done in one command on the whole list.

实际上,我希望看到它在单个字符串上完成,并且在整个列表中的一个命令中执行的方式有多么不同。

3 个解决方案

#1


24  

Either of these:

这两种:

gsub("MD|M\\.D\\.|PHD", "", test)  # target specific strings
gsub("\\,.+$", "", test)        # target all characters after comma

Both Matt Parker above and Tommy below have raised the question whether 'M.R.C.P.', 'PhD', 'D.Phil.' and 'Ph.D.' or other British or Continental designations of doctorate level degrees should be sought out and removed. Perhaps @user56 can advise what the intent was.

上面的马特·帕克和下面的汤米都提出了“M.R.C.P.”的问题”、“博士”、“哲学博士。“博士”和“博士”或其他英国或欧洲国家的博士学位应该被寻找并删除。也许@user56可以告诉我们意图是什么。

#2


3  

With a single ugly regex:

用一个丑陋的regex:

 gsub('[M,P].?D.?','',npt2$name)

Which says, find characters M or P followed by zero or one character of any kind, followed by a D and zero or one additional character. More explicitly, you could do this in three steps:

也就是说,找到M或P后面跟着0或任何类型的一个字符,后面跟着D和0或一个附加字符。更明确地说,您可以通过以下三个步骤来完成:

npt2$name <- gsub('MD','',npt2$name)
npt2$name <- gsub('M\\.D\\.','',npt2$name)
npt2$name <- gsub('PhD','',npt2name)

In those three, what's happening should be more straight forward. the second replacement you need to "escape" the period since its a special character.

在这三者中,发生的事情应该更直接。第二个替换你需要“转义”的时期,因为它是一个特殊的字符。

#3


2  

Here's a variant that removes the extra ", " too. Does not require touppper either - but if you want that, just specify ignore.case=TRUE to gsub.

这里有一个变体,去掉了额外的“,”。也不需要touppper——但是如果你需要的话,只需要指定ignore。案例gsub = TRUE。

test <- c("Andreas Groll, M.D.", 
  "",
  "Pan-Chyr Yang, PHD",
  "Suh-Fang Jeng, Sc.D",
  "Peter S Sebel, MB BS, PhD Chantal Kerssens, PhD",
  "Lawrence Currie, MD")

gsub(",? *(MD|M\\.D\\.|P[hH]D)", "", test)
#[1] "Andreas Groll"                         ""                                     
#[3] "Pan-Chyr Yang"                         "Suh-Fang Jeng, Sc.D"                  
#[5] "Peter S Sebel, MB BS Chantal Kerssens" "Lawrence Currie"

#1


24  

Either of these:

这两种:

gsub("MD|M\\.D\\.|PHD", "", test)  # target specific strings
gsub("\\,.+$", "", test)        # target all characters after comma

Both Matt Parker above and Tommy below have raised the question whether 'M.R.C.P.', 'PhD', 'D.Phil.' and 'Ph.D.' or other British or Continental designations of doctorate level degrees should be sought out and removed. Perhaps @user56 can advise what the intent was.

上面的马特·帕克和下面的汤米都提出了“M.R.C.P.”的问题”、“博士”、“哲学博士。“博士”和“博士”或其他英国或欧洲国家的博士学位应该被寻找并删除。也许@user56可以告诉我们意图是什么。

#2


3  

With a single ugly regex:

用一个丑陋的regex:

 gsub('[M,P].?D.?','',npt2$name)

Which says, find characters M or P followed by zero or one character of any kind, followed by a D and zero or one additional character. More explicitly, you could do this in three steps:

也就是说,找到M或P后面跟着0或任何类型的一个字符,后面跟着D和0或一个附加字符。更明确地说,您可以通过以下三个步骤来完成:

npt2$name <- gsub('MD','',npt2$name)
npt2$name <- gsub('M\\.D\\.','',npt2$name)
npt2$name <- gsub('PhD','',npt2name)

In those three, what's happening should be more straight forward. the second replacement you need to "escape" the period since its a special character.

在这三者中,发生的事情应该更直接。第二个替换你需要“转义”的时期,因为它是一个特殊的字符。

#3


2  

Here's a variant that removes the extra ", " too. Does not require touppper either - but if you want that, just specify ignore.case=TRUE to gsub.

这里有一个变体,去掉了额外的“,”。也不需要touppper——但是如果你需要的话,只需要指定ignore。案例gsub = TRUE。

test <- c("Andreas Groll, M.D.", 
  "",
  "Pan-Chyr Yang, PHD",
  "Suh-Fang Jeng, Sc.D",
  "Peter S Sebel, MB BS, PhD Chantal Kerssens, PhD",
  "Lawrence Currie, MD")

gsub(",? *(MD|M\\.D\\.|P[hH]D)", "", test)
#[1] "Andreas Groll"                         ""                                     
#[3] "Pan-Chyr Yang"                         "Suh-Fang Jeng, Sc.D"                  
#[5] "Peter S Sebel, MB BS Chantal Kerssens" "Lawrence Currie"