title: R: calculate rank sum automatically
body: <p>Given <code>x &lt;- cbind(c(10,15,20,20,25,30,30,30,35,40,40,40,40,45),rep(c('M','F'),7))</code>, I want to calculate the rank sums of of categories M and F <em>automatically</em>, without doing it by hand. The thing I couldn't figure out is how to adjust the rank numbers when there is a tie. In this case, #3 and #4 are both 20 and thus share the rank value of 3.5 (instead of 3 and 4). Likewise #6 ~ #8 have the rank value of 7, and #10 ~ #13 have 11.5. Without this adjustment, the sums would be wrong.</p>
  
  <p><code>#Wrong</code></p>
  
  <p><code>sum(which(x[,2]=='F')) # =56</code></p>
  
  <p><code>sum(which(x[,2]=='M')) # =49</code></p>
  
  <p><code>#Right</code></p>
  
  <p><code>sum(1,3.5,5,7,9,11.5,11.5) # =56.5</code> </p>
  
  <p><code>sum(2,3.5,7,7,11.5,11.5,14) # =48.5</code></p>
  
  <p>I've tried <code>table()</code> and <code>duplicated()</code>, but couldn't figure out how to piece things together. Any ideas?</p>
  
  <p>EDIT: My thanks to konvas for suggesting <code>rank()</code>, which works in addition to bgoldst's solution.</p>