title: Keep only sentences in corpus that contain specific key words (in R)
body: <p>I have a corpus with <code>.txt</code> documents. From these <code>.txt</code> documents, I do not need all sentences, but I only want to keep certain sentences that contain specific key words. From there on, I will perform similarity measures etc.</p>
  <p>So, here is an example.
  From the data_corpus_inaugural data set of the quanteda package, I only want to keep the sentences in my corpus that contain the words &quot;future&quot; and/or &quot;children&quot;.</p>
  <p>I load my packages and create the corpus:</p>
  <pre><code>library(quanteda)
  library(stringr)
  
  
  ## corpus with data_corpus_inaugural of the quanteda package
  corpus &lt;- corpus(data_corpus_inaugural)
  summary(corpus)
  </code></pre>
  <p>Then I want to keep only those sentences that contain my key words</p>
  <pre><code>## keep only those sentences of a document that contain words future or/and 
  children
  </code></pre>
  <p>First, let's see which documents contain these key words</p>
  <pre><code>## extract all matches of future or children
  str_extract_all(corpus, pattern = &quot;future|children&quot;)
  </code></pre>
  <p>So far, I only found out how to exclude the sentences that contain my key words, which is the opposite of what I want to do.</p>
  <pre><code>## excluded sentences that contains future or children or both (?)
  corpustrim &lt;- corpus_trimsentences(corpus, exclude_pattern = 
  &quot;future|children&quot;)
  summary(corpustrim)
  </code></pre>
  <p>The above command excludes sentences containing my key words.
  My idea here with the corpus_trimsentences function is to exclude all sentences BUT those containing &quot;future&quot; and/or &quot;children&quot;.</p>
  <p>I tried with regular expression. However, I did not manage to do it. It does not return what I want.</p>
  <p>I looked into the <code>corpus_reshape</code> and <code>corpus_subset</code> functions of the quanteda package but I can't figure out how to use them for my purpose.</p>