title: Mutating a list of strings to include K-means cluster number in R
body: <p>I am following <a href="https://medium.com/@SAPCAI/text-clustering-with-r-an-introduction-for-data-scientists-c406e7454e76" rel="nofollow noreferrer">this tutorial</a> and this is all super new to me, so apologies if it's an obvious question.</p>
  <p>Following the tutorial, I have converted a list of strings, <code>sentences</code>, into a K-Means model with 4 clusters using the following:</p>
  <pre><code>corpus = tm::Corpus(tm::VectorSource(sentences)) 
  tdm &lt;- tm::DocumentTermMatrix(corpus.cleaned) 
  tdm.tfidf &lt;- tm::weightTfIdf(tdm)
  tdm.tfidf &lt;- tm::removeSparseTerms(tdm.tfidf, 0.999) 
  tfidf.matrix &lt;- as.matrix(tdm.tfidf) 
  dist.matrix = proxy::dist(tfidf.matrix, method = &quot;cosine&quot;)
  model &lt;- kmeans(dist.matrix, centers = 4)
  </code></pre>
  <p>Now, I would like to go back to the original list of <code>sentences</code> and show next to each one which cluster it forms part of. For example:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>sentences</th>
  <th>cluster</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>Lorem ipsum dolor sit amet</td>
  <td>1</td>
  </tr>
  <tr>
  <td>Consectetur adipiscing elit</td>
  <td>2</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I've tried the following (using the dplyr package):</p>
  <pre><code>clustered &lt;- mutate(sentences, cluster = model$cluster)
  </code></pre>
  <p>and</p>
  <pre><code>clustered &lt;- mutate(df$sentences, cluster = model$cluster)
  </code></pre>
  <p>But obviously this doesn't work, because as R says, &quot;no applicable method for 'mutate' applied to an object of class &quot;character&quot;.</p>
  <p>Any ideas?</p>