title: Convert longitudinal data with time variables from wide to long format in r
body: <p>I have longitudinal data with multiple measurements on a number of objects over time. The data is in long format with hundreds of variables and cases:</p>
  
  <pre><code>Id    Time1   Measurement11  ...   Time2    Measurement21   ...
   1     50.4    23             ...   52.1    25               ...
   2     64.3    30             ...   67.9    35               ...
   3     70.1    20             ...   72.3    29               ...
  </code></pre>
  
  <p>I would like to convert it to wide format for plotting</p>
  
  <pre><code>Id    Time    Measurement1 ...
   1     50.4    23           ...
   1     52.1    25           ...
   2     64.3    30           ...
   2     67.9    35           ...
   3     70.1    20           ...
   4     72.3    29           ...
  </code></pre>
  
  <p>I checked resources on gather, melt, reshape, reshape2 but it seems they deal with situations where there are multiple time columns which all contain  measurements. So far I have not found a good way to convert the data while maintaing the pairwise dependency on time and measurement.</p>
  
  <p>My current solution is to write something like </p>
  
  <pre><code>attatch(data)
  temp1&lt;-bind_cols(Time1,Measurement11)
  temp2&lt;-bind_cols(Time2,Measurement21)
  
  wide_format_measurement1&lt;-bind_rows(temp1,temp2,...)
  </code></pre>
  
  <p>It works since there are only 5 time variables, but it does not seem very efficient. I could use unite to create pairs of data, then use gather, and finally use separate. But this is essentially the same as the bind_cols method. There must be a better way?</p>