title: In R how do you write a sparse matrix to a file?
body: <p>I have a sparse matrix A, generated as an output of glmnet function.
  When I print the matrix A, it shows all the entries and at the top it reads - </p>
  
  <pre><code>    1897 x 100 sparse Matrix of class "dgCMatrix"
     [[ suppressing 32 column names 's0', 's1', 's2' ... ]]
  </code></pre>
  
  <p>However, when I try to see the dimensions of the matrix it shows NULL:</p>
  
  <pre><code>&gt; dim(A)
  NULL
  </code></pre>
  
  <p>Thus if I use as.matrix to convert it into a regular matrix and write to a file I get an error:</p>
  
  <pre><code>as.matrix(fit$A[,1])
  Error in as.matrix(fit$A[, 1]) : 
    error in evaluating the argument 'x' in selecting a method for function 'as.matrix': Error in fit$A[, 1] : incorrect number of dimensions
  </code></pre>
  
  <p>How do I fetch the values in this sparse matrix and write to a file?</p>
  
  <p>I encounter this problem when I do multinomial regression (family = "multinomial") in the glmnet function. However this works fine when I am doing binomail regression (family = "binomial").</p>
  
  <p>Also, I have tried with writeMM function. That does not work either:</p>
  
  <pre><code>&gt; library('Matrix')
  &gt; writeMM(fit$A,file='test.txt')
  Error in (function (classes, fdef, mtable)  : 
    unable to find an inherited method for function 'writeMM' for signature '"list"'
  </code></pre>