title: converting R code snippet to use the Matrix package?
body: <p>I am not sure there are any R users out there, but just in case:</p>
  
  <p>I am a novice at R and was kindly "handed down" the following R code snippet:</p>
  
  <pre><code>Beta &lt;- exp(as.matrix(read.table('beta.transpose')))
  WordFreq &lt;- read.table('freq-matrix')
  WordProbs &lt;- WordFreq$V1 / sum(WordFreq)
  
  infile &lt;- file('freq-matrix')
  outfile &lt;- file('doc_topic_prob_matrix', 'w')
  
  open(infile)
  open(outfile)
  
  for (i in 1:93049) {
    vec &lt;- t(scan(infile, nlines=1))
    topics &lt;- (vec/WordProbs) %*% Beta
    write.table(topics, outfile, append=T, row.names=F, col.names=F)
    }
  </code></pre>
  
  <p>When I tried running this on my dataset, the system thrashed and swapped like crazy. Now I realize that has a simple reason: the file freq-matrix holds a large (22GB) matrix and I was trying to read it into memory.</p>
  
  <p>I have been told to use the <a href="http://cran.r-project.org/web/packages/Matrix/Matrix.pdf" rel="nofollow noreferrer">Matrix</a> package, because freq-matrix has many, many zeros all over the place and it handles such cases well. Will that help? If so, any hints on how to change this code would be most welcome. I have no R experience and just started reading through the introduction PDF available on the site.</p>
  
  <p>Many thanks</p>
  
  <p>~l</p>