title: R: reshape elements of a data frame keeping order of variables in sequence
body: <p>I'm struggling with reshaping 12 separate list elements [which are in data frame format] and having the values stay in the right sequential order. These are measurements by day, with <code>value 1</code> corresponding to the first day of the month measurement, and so on until <code>value 31</code>, the maximum possible days/month. This is in a horizontal. For missing measurements/months where there are fewer than 31 days, <code>-9999</code> appears. The <code>-9999</code> is not the problem here. </p>
  
  <pre><code>&gt; myplist[[1]]
       COOPID YEAR MONTH ELEMENT value 1 value 2 value 3 value 4 value 5 value 6 
  3    170100 1982     9    PRCP       0      70      15       0       0       0       
  8    170100 1982    10    PRCP       0      10       0       0       0       0       
  13   170100 1982    11    PRCP       2      13       0     170       0       5       
  18   170100 1982    12    PRCP       0       0       0       0       2       5       
  23   170100 1983     1    PRCP       2       0       0       0       0      10       
  28   170100 1983     2    PRCP   -9999       0       0      52       6       0  
  </code></pre>
  
  <p>My goal is to have the list elements oriented vertically so that each day has its own row, so it would look like this:</p>
  
  <pre><code>&gt; myplist[[1]]
      YEAR MONTH DAY PRCP
      1982     9   1    0
      1982     9   2   70
      1982     9   3   15
  </code></pre>
  
  <p>I tried this code:</p>
  
  <pre><code>melt(myplist[[1]], id.vars = c("COOPID", "YEAR", "MONTH", "ELEMENT"))
  </code></pre>
  
  <p>But it listed the <code>value 1</code> of each month rather than the desired sequence of <code>value 1</code>, <code>value 2</code>, ..., <code>value 31</code>.</p>
  
  <pre><code>      COOPID YEAR MONTH ELEMENT variable value
  1     170100 1982     9    PRCP  value 1     0
  2     170100 1982    10    PRCP  value 1     0
  3     170100 1982    11    PRCP  value 1     2
  4     170100 1982    12    PRCP  value 1     0
  5     170100 1983     1    PRCP  value 1     2
  </code></pre>
  
  <p>And this code returned this undesired output and error:</p>
  
  <pre><code>&gt; reshape(myplist[[1]], idvar = c("YEAR","MONTH"),varying =print(paste0("value",1:31)),sep = "",
  +         timevar = c("YEAR","MONTH"),direction = "long")
   [1] "value1"  "value2"  "value3"  "value4"  "value5"  "value6"  "value7"  "value8"  "value9"  "value10"
  [11] "value11" "value12" "value13" "value14" "value15" "value16" "value17" "value18" "value19" "value20"
  [21] "value21" "value22" "value23" "value24" "value25" "value26" "value27" "value28" "value29" "value30"
  [31] "value31"
  Error in `[.data.frame`(data, , varying.i) : undefined columns selected
  </code></pre>
  
  <p>I also tried dcast to no avail, and other asked questions on the this and other sites don't seem to get at the issue I'm having. I think the root of the problem is that my range of years has variable length days per month (goes to the actual last day of a given month), but each month in my measurement data has 31 day values regardless.</p>