title: Collapsing scattered information across multiple variables into 1 in R
body: <p>I have some table data that has been scattered across around 1000 variables in a dataset. Most are split across 2 variables, and I can piece together the data using coalesce, however this is pretty inefficient for some variables which are instead spread across &gt;10. Is there are a better/more efficient way?</p>
  <p>The syntax I have written so far is:</p>
  <pre><code>scattered_data &lt;- df %&gt;%
  select(id, contains(&quot;MASS9A_E2&quot;)) %&gt;% 
  #this brings in all the variables for this one question that start with this string
  mutate(speciality = coalesce(MASS9A_E2_C4_1,MASS9A_E2_C4_2,MASS9A_E2_C4_3, MASS9A_E2_C4_4, MASS9A_E2_C4_5, MASS9A_E2_C4_6, MASS9A_E2_C4_7, MASS9A_E2_C4_8, MASS9A_E2_C4_9, MASS9A_E2_C5_1,MASS9A_E2_C5_2,MASS9A_E2_C5_3, MASS9A_E2_C5_4, MASS9A_E2_C5_5, MASS9A_E2_C5_6, MASS9A_E2_C5_7, MASS9A_E2_C5_8, MASS9A_E2_C5_9))
  
  </code></pre>
  <p>As I have this for 28 MASS questions and would really love to be able to collapse these down a bit quicker.</p>