title: Renaming subsets of variables using the value of one of those variables, looped over an arbitrary number of subsets of variables
body: <p>I have a dataset that contains (in addition to observation id numbers) variables that each have only one value, which should be part of the name of several related variables (see example below). Here is a miniature version of the dataset:</p>
  
  <pre><code>df_input &lt;- tribble(
  ~id,  ~name_code1, ~count1, ~value1, ~name_code2,  ~count2, ~value2,
  1,    "A0000",       0,       0,       "A0001",         5,       200,
  2,    "A0000",       1,       5,       "A0001",         2,       50,
  3,    "A0000",       20,      3000,    "A0001",         3,       30, 
  )
  </code></pre>
  
  <p>So, each subset of variables has name_code#, count#, and value#, where variables are grouped by having the same number #. Note that for a given #, name_code# only ever takes one value, a unique identifier for that set of variables.  I would like, for an arbitrary number of #s, to be able to set the one value of name_code'i' as the prefix for each other 'i' variable's name, then remove the 'i' from the variable names. </p>
  
  <p>More succinctly, I would like to have this.... </p>
  
  <pre><code>tribble(
    ~id,   ~A0000_count, ~A0000_value, ~A0001_count, ~A0001_value,
  1,          0,              0,        5,           200,
  2,          1,              5,        2,           50,
  3,         20,           3000,        3,           30, 
  )
  </code></pre>
  
  <p>... and would like to be able to do so without manually renaming each variable (and deleting the "_code#" variables) individually, as I have several hundred numbered sets of variables.</p>
  
  <p>Really, I have no idea where to even begin beyond just individually renaming each variable. But in the real dataset I have several hundred <strong>sets</strong> of variables to rename this way, and want to minimize coding/spelling errors. Mostly, it's I have no idea how to:
  a) rename one variable using the value of another variable.
  b) loop over several sets of variables in the way described above.</p>
  
  <p>(Also: it doesn't have to be a tibble, I'm just doing that because it looks clearer to me visually).</p>
  
  <p>Thanks for your help!</p>