title: Conditional calculating the numbers of values in column with R
body: <p>I have two vectors:</p>
  
  <pre><code>x &lt;- c(1,1,1,1,1, 2,2,2,3,3,  3,3,3,4,4,  5,5,5,5,5 )
  y &lt;- c(2,2,1,3,2, 1,4,2,2,NA, 3,3,3,4,NA, 1,4,4,2,NA)
  </code></pre>
  
  <p>This question (<a href="https://stackoverflow.com/questions/24331593/conditional-calculating-the-numbers-of-values-in-column-with-r-part2">Conditional calculating the numbers of values in column with R, part2</a>) discussed how to find the number of values in <code>w</code> (don't count <code>NA</code>) for each <code>x</code> (from 1–5) and for each <code>y</code> (from 1–4).</p>
  
  <p>Let's split <code>X</code> by groups: if <code>x&lt;=2</code>, group <code>I</code>; if <code>2&lt;x&lt;=3</code>, group <code>II</code>; and if <code>3&lt;X&lt;=5</code>, group <code>III</code>. I need to find the number of different values in <code>x</code> by groups and by every value of <code>y</code>. I also need to find the mean of those values in <code>x</code> by the same groups. The output should be in this format:</p>
  
  <pre><code>y x    Result 1 (the number of distinct numbers in X); Result 2 (the mean)
  1 I     ...
  1 II    ...
  1 III   ...     
  ...
  4 I     ...
  4 II    ...
  4 III   ...
  </code></pre>