title: How to order columns and rows to create a relatively dense submatrix
body: <p>I have a large matrix which comprises 1,2 and missing (coded as NA) values. The matrix has 500000 rows by 10000 columns. There are approximately 0.05% 1- or 2-values, and the remaining values are NA.</p>
  
  <p>I would like to reorder the rows and columns of the matrix so that the top left corner of the matrix comprises a relatively high number of 1s and 2s compared to the rest of the matrix. In other words, I would like to create a relatively datarich subset of the matrix, by reordering the matrix rows and columns.</p>
  
  <p>Is there an efficient method of achieving this in R? </p>
  
  <p>In particular, I'm interested in solutions where sorting by the number of non-NA values in each row and column is not sufficient to produce a dense corner. </p>
  
  <p>In addition, I'll add a constraint. The size of the dense corner will be pre-defined. </p>
  
  <p>In the following example, the goal is to reorder the rows and columns so that the top leftmost 3x3 submatrix is relatively dense (i.e. few or no NA values).</p>
  
  <p><code>m1 &lt;- matrix(c(rep(c(rep(NA, 3), rep(1, 7)), 1),
                 rep(c(rep(2, 3), rep(NA, 7)), 7),
                 rep(c(rep(NA, 3), rep(1, 7)), 2)
                ), nrow=10, byrow=TRUE)</code></p>
  
  <pre><code>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
   [1,]   NA   NA   NA    1    1    1    1    1    1     1
   [2,]    2    2    2   NA   NA   NA   NA   NA   NA    NA
   [3,]    2    2    2   NA   NA   NA   NA   NA   NA    NA
   [4,]    2    2    2   NA   NA   NA   NA   NA   NA    NA
   [5,]    2    2    2   NA   NA   NA   NA   NA   NA    NA
   [6,]    2    2    2   NA   NA   NA   NA   NA   NA    NA
   [7,]    2    2    2   NA   NA   NA   NA   NA   NA    NA
   [8,]    2    2    2   NA   NA   NA   NA   NA   NA    NA
   [9,]   NA   NA   NA    1    1    1    1    1    1     1
  [10,]   NA   NA   NA    1    1    1    1    1    1     1
  </code></pre>
  
  <p>The rows and columns are ordered by the number of non-NA values (using code from an answer below): </p>
  
  <pre><code>m1 &lt;- m1[order(rowSums(is.na(m1))), order(colSums(is.na(m1)))]
  </code></pre>
  
  <p>However, this does not result in a dense 3x3 top leftmost corner:</p>
  
  <pre><code>         [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
      [1,]   NA   NA   NA    1    1    1    1    1    1     1
      [2,]   NA   NA   NA    1    1    1    1    1    1     1
      [3,]   NA   NA   NA    1    1    1    1    1    1     1
      [4,]    2    2    2   NA   NA   NA   NA   NA   NA    NA
      [5,]    2    2    2   NA   NA   NA   NA   NA   NA    NA
      [6,]    2    2    2   NA   NA   NA   NA   NA   NA    NA
      [7,]    2    2    2   NA   NA   NA   NA   NA   NA    NA
      [8,]    2    2    2   NA   NA   NA   NA   NA   NA    NA
      [9,]    2    2    2   NA   NA   NA   NA   NA   NA    NA
     [10,]    2    2    2   NA   NA   NA   NA   NA   NA    NA
  </code></pre>
  
  <p>I thought that there maybe a set of optimisation procedures that I could implement as my working matrix is too large to do the reorganisation by eye.</p>