title: Data rows, variables and values get reordered during computations
body: <p>I have a problem with R when adding my data to a matrix prior to drawing:</p>
  
  <pre><code>&gt; resFile &lt;- read.csv("file.csv")
  &gt; print(resFile)
       Gene Virus  Expression    Percentage
  1    ga   1Virus 2.738598e-02  38.590745
  2    ga   2Virus 3.247252e-02  64.331929
  3    ga   PIC    4.235604e-02  114.348940
  4    ga   MOCK   1.976032e-02  0.000000        
  &gt; samples &lt;- unique(resFile$Virus)
  &gt; genes &lt;- unique(resFile$Gene)
  &gt; numGene &lt;- length(genes)
  &gt; numSmpl &lt;- length(samples)
  
  &gt; mat &lt;- matrix(ncol=numGene,nrow=numSmpl,dimnames=list(samples,genes))
  &gt; mat[samples,genes]&lt;-resFile$Percentage
  &gt; print(mat)
                ga 
  1Virus  38.59074
  2Virus  64.33193
  PIC      0.00000
  MOCK   114.34894
  </code></pre>
  
  <p>As you can see, the <code>percentage</code> values are switched between my PIC and MOCK samples. This happens to whole columns as well and it looks like the values are added in alphabetical order instead of indicially. </p>
  
  <p>Why is this happening and how can I get around it?</p>