title: How to randomize (or permute) a dataframe rowwise and columnwise?
body: <p>I have a dataframe (df1) like this.</p>
  
  <pre><code>     f1   f2   f3   f4   f5
  d1   1    0    1    1    1  
  d2   1    0    0    1    0
  d3   0    0    0    1    1
  d4   0    1    0    0    1
  </code></pre>
  
  <p>The d1...d4 column is the rowname, the f1...f5 row is the columnname.</p>
  
  <p>To do sample(df1), I get a new dataframe with count of 1 same as df1.  So, the count of 1 is conserved for the whole dataframe but not for each row or each column.</p>
  
  <p>Is it possible to do the randomization row-wise or column-wise?</p>
  
  <p>I want to randomize the df1 column-wise for each column, i.e. the number of 1 in each column remains the same. and each column need to be changed by at least once.  For example, I may have a randomized df2 like this:  (Noted that the count of 1 in each column remains the same but the count of 1 in each row is different.</p>
  
  <pre><code>     f1   f2   f3   f4   f5
  d1   1    0    0    0    1  
  d2   0    1    0    1    1
  d3   1    0    0    1    1
  d4   0    0    1    1    0
  </code></pre>
  
  <p>Likewise, I also want to randomize the df1 row-wise for each row, i.e. the no. of 1 in each row remains the same, and each row need to be changed (but the no of changed entries could be different).   For example, a randomized df3 could be something like this:</p>
  
  <pre><code>     f1   f2   f3   f4   f5
  d1   0    1    1    1    1  &lt;- two entries are different
  d2   0    0    1    0    1  &lt;- four entries are different
  d3   1    0    0    0    1  &lt;- two entries are different
  d4   0    0    1    0    1  &lt;- two entries are different
  </code></pre>
  
  <p>PS.  Many thanks for the help from Gavin Simpson, Joris Meys and Chase for the previous answers to my previous question on randomizing two columns.</p>