【问题标题】:using R How do I retain a sentence with a key word使用R如何保留带有关键字的句子
【发布时间】:2016-02-18 05:54:18
【问题描述】:

我有如下所示的数据集

   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

非常感谢任何帮助。

【问题讨论】:

  • 这些句子在'Id'中是否用\n分隔`
  • @akrun,一些来自.,一些来自\n

标签: r string grepl keyword-search


【解决方案1】:

我们可以使用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"

【讨论】:

  • @akurn,如果句子仅由一个 . 分隔,它可以工作,但是如果句子被随机点分隔怎么办,我的意思是,有些是 . 有些是 .... 有些是..
  • @ErnestPresley 我只能根据您展示的示例进行检查。根据您显示的预期输出,它可以工作。
  • @ErnestPresley 请用这种情况编辑您的问题。
  • @akrun,对不起,我现在更新了一些随机点场景的问题。
  • @akrun,快乐的结局:)
猜你喜欢
  • 1970-01-01
  • 2017-01-21
  • 2012-08-03
  • 1970-01-01
  • 2020-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多