title: tidy rearrange within rows
body: <p>Is there a tidier way than the following to "rearrange" values based on a second column?</p> 
  
  <p>The df contains responses to likert questions that were presented in a randomised order in a questionnaire. The "item" columns contain which item was presented, the "response" columns contain the answers. </p> 
  
  <p> The goal is to have a df where response columns are sorted by the item column for each participant (row)? </p>
  
  <p>Initial Format:</p>
  
  <pre><code> ##     id  item.1  response.1  item.2  response.2  item.3  response.3  ... item.x   respons.x
   ##  1  1     2        5           1        4          3         2      
   ##  2  2     3        4           1        5          2         4       
   ## ...
   ##  i    
  </code></pre>
  
  <p>Desired Format:</p>
  
  <pre><code> ##    id  order_1 reponse_1 order_2 reponse_2  order_3 reponse_3 ...  order_x   reponse_x
   ## 1  1     2         4         1        5        3        2          
   ## 2  2     2         5         3        4        1        4          
   ## ...
   ## i         
  </code></pre>
  
  <p>So far my tidiest approach is the following:</p>
  
  <pre><code>df &lt;- data_frame(
    id = 1:4,
    item.1 = c(2,3,2,4),
    response.1 = c(4,1,3,2),
    item.2 = c(1,2,1,1),
    response.2 = c(5,4,5,4),
    item.3 = c(3,4,3,2),
    response.3 = c(1,2,2,5),
    item.4 = c(4,1,4,3),
    response.4 = c(2,2,2,1)
  )
  
  df_long &lt;- df %&gt;%
    unite(col = one, ends_with(".1"), sep = ";") %&gt;%
    unite(col = two, ends_with(".2"), sep = ";") %&gt;%
    unite(col = three, ends_with(".3"), sep = ";") %&gt;%
    unite(col = four, ends_with(".4"), sep = ";") %&gt;%
    gather(key = "number", value = "item_response", c(one, two, three, four)) %&gt;%
    arrange(id) %&gt;%
    separate(item_response, into = c("item", "response"))
  </code></pre>
  
  <p>This initial step could have also be done with less lines of code with the base function reshape(). </p>
  
  <pre><code>reshape(df, varying = names(df[,-1]), idvar = "id", direction = "long")
  </code></pre>
  
  <p>I would however prefer a tidyverse solution. I found this thread (<a href="https://github.com/tidyverse/tidyr/issues/150" rel="nofollow noreferrer">https://github.com/tidyverse/tidyr/issues/150</a>) with a discussion of having a multi-gather, but it seems that there hasn't been a final solution yet. <p> In the end I do not even want my data to be in long format and therefore need to spread it back to wide.</p></p>
  
  <pre><code>df_final &lt;- df_long %&gt;%
    unite(col = order_response, c(number,response), sep = ";") %&gt;%
    spread(item, order_response) %&gt;%
    separate(`1`, into = c("order_1", "response_1"), sep = ";") %&gt;%
    separate(`2`, into = c("order_2", "response_2"), sep = ";") %&gt;%
    separate(`3`, into = c("order_3", "response_3"), sep = ";") %&gt;%    
    separate(`4`, into = c("order_4", "response_4"), sep = ";")  
  </code></pre>
  
  <p>In the df I am using I however have 40 items instead of 4, which adds quiet some lines to the code. Is there a tidier way? I have not worked with purr a lot, but is this something purr could help with? To me the problem seems quiet straightforward and I am suprised that I could not find a better solution for it so far.</p>