title: R: Changing column names in pivot_wider() -- suffix to prefix
body: <p>I'm trying to figure out how to alter the way in which tidyr's <code>pivot_wider()</code> function creates new variable names in resulting wide data sets. Specifically, I would like the &quot;names_from&quot; variable to be added to the prefix of the new variables rather than the suffix.</p>
  <p>My data set looks like:</p>
  <pre><code>list(ID = c(&quot;A950&quot;, &quot;A950&quot;, &quot;A950&quot;, &quot;A970&quot;, &quot;A970&quot;, &quot;A970&quot;, &quot;A996&quot;, &quot;A996&quot;, &quot;A996&quot;), 
  Phase = c(&quot;P1&quot;, &quot;P2&quot;, &quot;P3&quot;, &quot;P1&quot;, &quot;P2&quot;, &quot;P3&quot;, &quot;P1&quot;, &quot;P2&quot;, &quot;P3&quot;), 
  A = c(23.5, 25.2, 21.9, 21.9, 21.1, 20.3, 19.5, 18.7, 17.9), 
  B = c(21.9, 21.1, 20.3, 19.5, 18.7, 17.9, 17.1, 16.3, 15.5), 
  C = c(25.2, 21.9, 20.3, 17.6, 15.1, 12.7, 10.3, 7.8, 5.4), 
  D = c(&quot;M&quot;, &quot;M&quot;, &quot;M&quot;, &quot;F&quot;, &quot;F&quot;, &quot;F&quot;, &quot;N&quot;, &quot;N&quot;, &quot;N&quot;))
  </code></pre>
  <p>When I use <code>pivot_wider()</code> to spread the data set using Phase as the &quot;key&quot;, my results look like:</p>
  <pre><code>ex_wide &lt;- ex_long %&gt;%
    pivot_wider(names_from = Phase, values_from = c(3:6))
  
  list(ID = c(&quot;A950&quot;, &quot;A970&quot;, &quot;A996&quot;), 
  A_P1 = c(23.5, 21.9, 19.5), 
  A_P2 = c(25.2, 21.1, 18.7), 
  A_P3 = c(21.9, 20.3, 17.9), 
  B_P1 = c(21.9, 19.5, 17.1), 
  B_P2 = c(21.1, 18.7, 16.3), 
  B_P3 = c(20.3, 17.9, 15.5), 
  C_P1 = c(25.2, 17.6, 10.3), 
  C_P2 = c(21.9, 15.1, 7.8), 
  C_P3 = c(20.3, 12.7, 5.4), 
  D_P1 = c(&quot;M&quot;, &quot;F&quot;, &quot;N&quot;), 
  D_P2 = c(&quot;M&quot;, &quot;F&quot;, &quot;N&quot;), 
  D_P3 = c(&quot;M&quot;, &quot;F&quot;, &quot;N&quot;))
  </code></pre>
  <p>What I would like is for the column names to look like <strong>P1_A</strong> rather than <strong>A_P1</strong> (i.e., phase_variable rather than variable_phase).</p>
  <p>This seems like it would be simple to figure out; however, I have not been able to find any solutions that fit my needs. Any help would be greatly appreciated. Thanks in advance.</p>