title: R - Selecting columns based on a condition
body: <p>I am trying to find an easy way to select different columns (<code>db$Item1</code>, <code>db$Item2</code>, <code>db$Item3</code>, <code>db$Item4</code>) from a data set based on a condition (<code>db$Scale</code>). Preferably using dplyr. Here's a reproducible example.</p>
  
  <pre><code>Scale &lt;- c(1, 1, 1, 2, 2, 3, 3)
  Item1 &lt;- c(4, 5, 4, 6, 7, 7, 6)
  Item2 &lt;- c(2, 3, 6, 3, 4, 5, 4)
  Item3 &lt;- c(6, 7, 3, 4, 5, 6, 5)
  Item4 &lt;- c(2, 5, 3, 5, 2, NA, NA)
  
  db &lt;- data.frame(Scale, Item1, Item2, Item3, Item4)
  </code></pre>
  
  <p>Brief description: I have a data set with three different scales (1, 2, and 3) each of which has a different number of items. The scale 1 is composed of 4 items, the scale 2 is composed of 4 items, and the scale 3 is composed of 3 items.</p>
  
  <pre><code>#  Scale Item1 Item2 Item3 Item4
  #1     1     4     2     6     2
  #2     1     5     3     7     5
  #3     1     4     6     3     3
  #4     2     6     3     4     5
  #5     2     7     4     5     2
  #6     3     7     5     6    NA
  #7     3     6     4     5    NA
  </code></pre>
  
  <p>What I need to do is to create a new data set (let's say: <code>db.X</code>) for which I have the old column <code>Scale</code> (new name: <code>Scale.X</code>), and then a new column <code>Item1.X</code> composed of the values under the column <code>Item 1</code> for the scales 1 and 2, and the values under the column <code>Item 3</code> for the scale 3. Also, I want to report the values for <code>db$Item3</code> under the new column <code>Item2.X</code> </p>
  
  <pre><code>#  Scale.X Item1.X Item2.X
  #1      1      4      6
  #2      1      5      7
  #3      1      4      3
  #4      2      3      4
  #5      2      4      5
  #6      3      5      6
  #7      3      4      5
  </code></pre>