1

我有如下所示的数据集

   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

我想保留包含关键字How且不区分大小写的句子

最终数据集应如下所示

   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

任何帮助深表感谢。

4

1 回答 1

2

我们可以用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"
于 2016-02-18T05:58:01.157 回答