title: how to reformat the factor data in r
body: <p>I have a data frame like below,</p>
  <pre><code>     structure(list(habitat = c(&quot;open&quot;, &quot;open&quot;, &quot;open&quot;, &quot;open&quot;, &quot;open&quot;, 
  &quot;open&quot;, &quot;open&quot;, &quot;open&quot;, &quot;open&quot;, &quot;open&quot;, &quot;open&quot;, &quot;open&quot;, &quot;open&quot;, 
  &quot;open&quot;, &quot;open&quot;, &quot;open&quot;, &quot;open&quot;, &quot;open&quot;, &quot;open&quot;, &quot;open&quot;, &quot;open&quot;, 
  &quot;open&quot;, &quot;open&quot;, &quot;open&quot;, &quot;open&quot;, &quot;open&quot;, &quot;open&quot;, &quot;open&quot;, &quot;open&quot;, 
  &quot;open&quot;, &quot;open&quot;, &quot;open&quot;, &quot;open&quot;, &quot;open&quot;, &quot;open&quot;, &quot;overgrown&quot;, 
  &quot;overgrown&quot;, &quot;overgrown&quot;, &quot;overgrown&quot;, &quot;overgrown&quot;, &quot;overgrown&quot;, 
  &quot;overgrown&quot;, &quot;overgrown&quot;, &quot;overgrown&quot;, &quot;overgrown&quot;, &quot;overgrown&quot;, 
  &quot;overgrown&quot;, &quot;overgrown&quot;, &quot;overgrown&quot;, &quot;overgrown&quot;, &quot;overgrown&quot;, 
  &quot;overgrown&quot;, &quot;overgrown&quot;, &quot;overgrown&quot;, &quot;overgrown&quot;, &quot;overgrown&quot;, 
  &quot;overgrown&quot;, &quot;overgrown&quot;, &quot;overgrown&quot;, &quot;overgrown&quot;, &quot;overgrown&quot;, 
  &quot;overgrown&quot;, &quot;overgrown&quot;, &quot;overgrown&quot;, &quot;overgrown&quot;, &quot;overgrown&quot;, 
  &quot;overgrown&quot;, &quot;overgrown&quot;, &quot;overgrown&quot;, &quot;overgrown&quot;, &quot;overgrown&quot;, 
  &quot;overgrown&quot;, &quot;overgrown&quot;, &quot;overgrown&quot;, &quot;overgrown&quot;, &quot;overgrown&quot;, 
  &quot;overgrown&quot;, &quot;overgrown&quot;, &quot;overgrown&quot;, &quot;overgrown&quot;), sex = c(&quot;female&quot;, 
  &quot;female&quot;, &quot;female&quot;, &quot;female&quot;, &quot;male&quot;, &quot;female&quot;, &quot;female&quot;, &quot;female&quot;, 
  &quot;female&quot;, &quot;female&quot;, &quot;female&quot;, &quot;male&quot;, &quot;female&quot;, &quot;male&quot;, &quot;female&quot;, 
  &quot;female&quot;, &quot;female&quot;, &quot;female&quot;, &quot;female&quot;, &quot;female&quot;, &quot;female&quot;, &quot;male&quot;, 
  &quot;female&quot;, &quot;female&quot;, &quot;male&quot;, &quot;male&quot;, &quot;male&quot;, &quot;male&quot;, &quot;female&quot;, 
  &quot;female&quot;, &quot;female&quot;, &quot;female&quot;, &quot;male&quot;, &quot;female&quot;, &quot;male&quot;, &quot;male&quot;, 
  &quot;male&quot;, &quot;male&quot;, &quot;male&quot;, &quot;male&quot;, &quot;male&quot;, &quot;male&quot;, &quot;male&quot;, &quot;male&quot;, 
  &quot;male&quot;, &quot;male&quot;, &quot;male&quot;, &quot;male&quot;, &quot;male&quot;, &quot;female&quot;, &quot;male&quot;, &quot;male&quot;, 
  &quot;female&quot;, &quot;male&quot;, &quot;male&quot;, &quot;male&quot;, &quot;male&quot;, &quot;male&quot;, &quot;male&quot;, &quot;male&quot;, 
  &quot;male&quot;, &quot;male&quot;, &quot;male&quot;, &quot;male&quot;, &quot;male&quot;, &quot;male&quot;, &quot;male&quot;, &quot;male&quot;, 
  &quot;male&quot;, &quot;male&quot;, &quot;male&quot;, &quot;male&quot;, &quot;female&quot;, &quot;male&quot;, &quot;male&quot;, &quot;male&quot;, 
  &quot;male&quot;, &quot;female&quot;, &quot;male&quot;, &quot;female&quot;), colour = c(&quot;red&quot;, &quot;red&quot;, 
  &quot;red&quot;, &quot;red&quot;, &quot;red&quot;, &quot;red&quot;, &quot;red&quot;, &quot;red&quot;, &quot;red&quot;, &quot;red&quot;, &quot;red&quot;, 
  &quot;black&quot;, &quot;red&quot;, &quot;black&quot;, &quot;black&quot;, &quot;red&quot;, &quot;black&quot;, &quot;red&quot;, &quot;red&quot;, 
  &quot;black&quot;, &quot;black&quot;, &quot;black&quot;, &quot;red&quot;, &quot;red&quot;, &quot;red&quot;, &quot;black&quot;, &quot;black&quot;, 
  &quot;black&quot;, &quot;red&quot;, &quot;red&quot;, &quot;red&quot;, &quot;black&quot;, &quot;red&quot;, &quot;red&quot;, &quot;black&quot;, 
  &quot;black&quot;, &quot;red&quot;, &quot;black&quot;, &quot;red&quot;, &quot;red&quot;, &quot;black&quot;, &quot;black&quot;, &quot;black&quot;, 
  &quot;black&quot;, &quot;black&quot;, &quot;black&quot;, &quot;black&quot;, &quot;red&quot;, &quot;red&quot;, &quot;black&quot;, &quot;black&quot;, 
  &quot;black&quot;, &quot;red&quot;, &quot;red&quot;, &quot;red&quot;, &quot;black&quot;, &quot;red&quot;, &quot;red&quot;, &quot;black&quot;, 
  &quot;black&quot;, &quot;black&quot;, &quot;black&quot;, &quot;black&quot;, &quot;black&quot;, &quot;black&quot;, &quot;black&quot;, 
  &quot;black&quot;, &quot;black&quot;, &quot;black&quot;, &quot;black&quot;, &quot;black&quot;, &quot;black&quot;, &quot;red&quot;, 
  &quot;black&quot;, &quot;black&quot;, &quot;black&quot;, &quot;black&quot;, &quot;red&quot;, &quot;red&quot;, &quot;red&quot;)), class = &quot;data.frame&quot;, row.names = c(NA, 
  -80L))
  </code></pre>
  <p>I want to count the numbers based on 'habitat' and 'colour' columns and also reshape the data frame like below (despite the 'sex' column),</p>
  <pre><code>habitat        red  black
  
  open           24     11
  
  overgrown      14      31
  </code></pre>
  <p>How to realise this in r?</p>