title: How to calculate relative frequency of array elements using Ramda.js?
body: <p>I have an array with repeating values, and I want to get the relative frequency (i.e., proportions) for each repeating value.</p>
  <p>It seems natural to me to treat this as a 2-step procedure:</p>
  <ol>
  <li><em>count</em> the occurrences of each value; and</li>
  <li>divide that count by the <em>length</em> of the original array.</li>
  </ol>
  <p>To accomplish the first step we can use <code>R.countBy()</code> from <code>ramda.js</code>:</p>
  <pre class="lang-js prettyprint-override"><code>const R = require(&quot;ramda&quot;)
  
  const myLetters = [&quot;a&quot;, &quot;a&quot;, &quot;a&quot;, &quot;a&quot;, &quot;b&quot;, &quot;b&quot;, &quot;c&quot;, &quot;c&quot;, &quot;c&quot;, &quot;c&quot;]
  const counted = R.countBy(R.identity)(myLetters)
  counted // =&gt; gives {&quot;a&quot;: 4, &quot;b&quot;: 2, &quot;c&quot;: 4}
  </code></pre>
  <p>Now the second step would be to divide <code>counted</code> by the length of <code>myLetters</code>:</p>
  <pre class="lang-js prettyprint-override"><code>counted / R.length(myLetters) // obviously this doesn't work because it's not mapped
  </code></pre>
  <p>I'm a bit lost with how to map this correctly. My current clunky solution that I dislike:</p>
  <pre class="lang-js prettyprint-override"><code>// 1. manually calculate the length and store to a variable
  const nvals = R.length(myLetters)
  
  // 2. create a custom division function
  const divide_by_length = (x) =&gt; R.divide(x, nvals)
  
  // 3. map custom function to `counted`
  R.map(divide_by_length, counted) // gives {&quot;a&quot;: 0.4, &quot;b&quot;: 0.2, &quot;c&quot;: 0.4}
  </code></pre>
  <p>Although this works, there's gotta be a more straightforward way with <code>ramda</code> to get from <code>counted</code> to <code>{&quot;a&quot;: 0.4, &quot;b&quot;: 0.2, &quot;c&quot;: 0.4}</code>.</p>