首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在字符串中查找特定的关键字并将其替换为现有的字符串值?

如何在字符串中查找特定的关键字并将其替换为现有的字符串值?
EN

Stack Overflow用户
提问于 2018-03-28 16:33:36
回答 1查看 29关注 0票数 1

我有这样的数据

代码语言:javascript
复制
ID <- c("A112","A114","A134","A116","A117","A138")
Comment <- c("Beam calibration again", "Beam calibration. Tools ready",
             "Did not find anything wrong. Beam calibration","Performed beam calibration. tool ready",
             "STD Qual and Blurry image looks fine","STD Qual failed and Slightly Blurry image")
df<- data.frame(ID,Comment)
df

    ID                                      Comment
  A112                       Beam calibration again
  A114               Beam calibration. Tools ready
  A134 Did not find anything wrong. Beam calibration
  A116       Performed beam calibration. tool ready
  A117         STD Qual and Blurry image looks fine
  A138    STD Qual failed and Slightly Blurry image

由于评论太长,我想减少它来选择特定的关键字,如“模糊图像”,“光束校准”。我希望我的想要的输出

代码语言:javascript
复制
    ID          Comment
  A112 Beam calibration
  A114 Beam calibration
  A134 Beam calibration
  A116 beam calibration
  A117     Blurry image
  A138     blurry image

对于一个列,我尝试了这种方法,但是如何以编程方式对所有列应用类似的逻辑呢?

代码语言:javascript
复制
df$Comment <- gsub("Beam calibration again", "Beam calibration", df$Comment)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-28 16:46:08

给定您的示例数据,如果您只是在寻找Beam calibrationBlurry image,您可以这样做:

代码语言:javascript
复制
df$Comment <- gsub(".*(Beam calibration|Blurry image).*", "\\1", df$Comment, ignore.case = TRUE)

df
#    ID          Comment
#1 A112 Beam calibration
#2 A114 Beam calibration
#3 A134 Beam calibration
#4 A116 beam calibration
#5 A117     Blurry image
#6 A138     Blurry image

或者为了避免手工打字,如果你有一个关键字向量,你可以建立你的查找如下:

代码语言:javascript
复制
keywords <- c("Beam calibration", "Blurry image")
lookup <- paste0(".*(", paste(keywords, collapse = "|"), ").*")
df$Comment <- gsub(lookup, "\\1", df$Comment, ignore.case = TRUE)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49539824

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档