title: Long to wide format by collecting multiple ID columns in R which have different values
body: <p>I know there are a lot of guides on moving from long to wide format. For instance, see <a href="https://stackoverflow.com/questions/ask">here</a>. </p>
  
  <p>However, I believe I have a unique case, or at least a case that I cannot find on stack overflow with an answer, though maybe I do not have the right language for my question. I will depict my problem with data. I have a dataframe which looks like this: </p>
  
  <pre><code>&gt;  my.df &lt;- data.frame(ID=rep(c("A","B","C"), 3), TIME=rep(1:9, each=1), X=1:9, Y=10:18)
  &gt;  my.df
    ID TIME X  Y
  1  A    1 1 10
  2  B    2 2 11
  3  C    3 3 12
  4  A    4 4 13
  5  B    5 5 14
  6  C    6 6 15
  7  A    7 7 16
  8  B    8 8 17
  9  C    9 9 18
  </code></pre>
  
  <p>I want to move from long to wide format, but I importantly want to keep the unique information contained in the time column and do not want it to spread out into a column name.  I want to create multiple time columns where there is a duplicate of ID, and store the corresponding X and Y data next to that column.</p>
  
  <p>See below desired output:</p>
  
  <pre><code>&gt; my.df &lt;- data.frame(ID=c("A","B","C"), TIME_1=c(1,2,3),X_1 = c(1,2,3), Y_1= c(10,11,12),
  +                     TIME_2 = c(4,5,6),X_2 = c(4,5,6),Y_2 = c(13,14,15),
  +                     TIME_3 = c(7,8,9),X_3 = c(7,8,9),Y_3 = c(16,17,18))
  &gt; my.df
    ID TIME_1 X_1 Y_1 TIME_2 X_2 Y_2 TIME_3 X_3 Y_3
  1  A      1   1  10      4   4  13      7   7  16
  2  B      2   2  11      5   5  14      8   8  17
  3  C      3   3  12      6   6  15      9   9  18
  </code></pre>
  
  <p>Is this possible? </p>
  
  <p>My real dataframe is much bigger, and the TIME column contains unique dates that I can't put into a column name, which is why I want to keep that information stored in new columns.  I know that I only have at most 4 duplicate ID values, so I am not worried about creating an exorbitant amount of columns.</p>