简体   繁体   English

使用R如何保留带有关键字的句子

[英]using R How do I retain a sentence with a key word

I have dataset as shown below 我有如下所示的数据集

   Id     Comments
   1      How. will the binary giant change. 
          Will the unknown disregard maximize another blamed bottle?
   2      The thinking accent hurts.... How.. the lord coast?
   3      The panda moans about the intuitive room past a device.
   4      In an ideology punts the center..
          How. An exercise elaborates past a photographic bookshop

I want to retain sentences that contain the keyword How and not case sensitive 我想保留包含关键字How并且不区分大小写的句子

The final dataset should look like this 最终数据集应如下所示

   Id     Comments
   1      How will the binary giant change. 
   2      How the lord coast?
   3      NA
   4      How An exercise elaborates past a photographic bookshop

Any help is much appreciated. 任何帮助深表感谢。

We can use str_extract 我们可以使用str_extract

gsub('[.](?!$)', '', 
       str_extract(df1$Comments, "\\bHow.*(\\?|\\.|$)"), perl=TRUE)
#[1] "How will the binary giant change."                       
#[2] "How the lord coast?"                                    
#[3] NA                                                       
#[4] "How An exercise elaborates past a photographic bookshop"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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