title: Using reshape() function in R -- from wide to long
body: <p>I'm trying to re-arrange the data in R from something like:</p>
  
  <pre><code>Patient ID,Episode Number,Admission Date (A),Admission Date (H),Admission Time (A),Admission Time (H)
  1,5,20/08/2011,21/08/2011,1200,1300
  2,6,21/08/2011,22/08/2011,1300,1400
  3,7,22/08/2011,23/08/2011,1400,1500
  4,8,23/08/2011,24/08/2011,1500,1600
  </code></pre>
  
  <p>to something like:</p>
  
  <pre><code>Record Type,Patient ID,Episode Number,Admission Date,Admission Time
  H,1,5,20/08/2011,1200
  A,1,5,21/08/2011,1300
  H,2,6,21/08/2011,1300
  A,2,6,22/08/2011,1400
  H,3,7,22/08/2011,1400
  A,3,7,23/08/2011,1500
  H,4,8,23/08/2011,1500
  A,4,8,24/08/2011,1600
  </code></pre>
  
  <p>(I've used CSV format so it's easier to to use them as test data).</p>
  
  <p>I tried the reshape() function and it kind of worked:</p>
  
  <pre><code>&gt; reshape(foo, direction = "long", idvar = 1, varying = 3:dim(foo)[2], 
  &gt; sep = "..", timevar = "dataset")
       Patient.ID Episode.Number dataset Admission.Date Admission.Time
  1.A.          1              5      A.     20/08/2011           1200
  2.A.          2              6      A.     21/08/2011           1300
  3.A.          3              7      A.     22/08/2011           1400
  4.A.          4              8      A.     23/08/2011           1500
  1.H.          1              5      H.     21/08/2011           1300
  2.H.          2              6      H.     22/08/2011           1400
  3.H.          3              7      H.     23/08/2011           1500
  4.H.          4              8      H.     24/08/2011           1600
  </code></pre>
  
  <p>But it's not in the exact format I wanted (I want for each "Patient ID", the first row is "H" and second row is "A").</p>
  
  <p>In addition, when I extend this to the read data (which has 250+ columns) it failed:</p>
  
  <pre><code>&gt; reshape(realdata, direction = "long", idvar = 1, varying = 
  &gt; 6:dim(foo)[2], sep = "..", timevar = "dataset")
  Error in reshapeLong(data, idvar = idvar, timevar = timevar, varying = varying,  : 
    'varying' arguments must be the same length
  </code></pre>
  
  <p>I think partly because the colnames look like:</p>
  
  <pre><code>&gt; colnames(foo)
    [1] "Unique.Key"                                    
    [2] "Campus.Code"                                   
    [3] "UR"                                            
    [4] "Terminal.digit"                                
    [5] "Admission.date..A."                      
    [6] "Admission.date..H."                     
    [7] "Admission.time..A."                      
    [8] "Admission.time..H."     
    .
    .
    .
   [31] "Medicare.Number"                               
   [32] "Payor"                                         
   [33] "Doctor.specialty"                              
   [34] "Clinic"     
    .
    .
    .
   [202] "Admission.Source..A."                    
   [203] "Admission.Source..H."  
  </code></pre>
  
  <p>i.e. there are "common columns" (without suffixes) between columns with suffixes (hope this makes sense).</p>