title: Assigning Output of For Loop to Variable in R
body: <p>I'm trying to get the following output in R.</p>
  
  <p><strong>E.g.: Cluster 1: opportunities career learning</strong></p>
  
  <pre><code>result &lt;- vector("list",k)
  for (i in 1:k) {
      s &lt;- sort(kmeansResult$centers[i,], decreasing=T)
      result[[i]] &lt;- print(paste("Cluster ", i, ": ", sep="", names(s)[1:3]))
  }
  result
  </code></pre>
  
  <p>However, in my above code, I keep getting "Cluster 1: opportunities" "Cluster 1: learning"      "Cluster 1: career".</p>
  
  <p>The following code works well (Output is - <strong>Cluster 1: opportunities learning career</strong>), but I need to assign the output to a variable (probably a list?) for printing into a HTML file. How can I assign the output of the entire for loop into a variable if I do it this way?</p>
  
  <pre><code>for (i in 1:k) {
      cat(paste("Cluster ", i, ": ", sep=""))
      s &lt;- sort(kmeansResult$centers[i,], decreasing=T)
      cat(names(s)[1:3], "\n")
  }
  </code></pre>