title: How to convert column values to rows for each unique value in a dataframe in R?
body: <p>I've a large dataframe which contains 12 columns each for two types of values, Rested and Active. I want to convert the columns of each month into rows, thus bring all the month columns (Jan, Feb, Mar... ) under 'Month'</p>
  
  <p>My data is as follows:</p>
  
  <pre><code>ID      L1  L2  Year    JR  FR  MR  AR  MYR JR  JLR AGR SR  OR  NR  DR  JA  FA  MA  AA  MYA JA  JLA AGA SA  OA  NA  DA
  1234    89  65  2003    11  34  6   7   8   90  65  54  3   22  55  66  76  86  30  76  43  67  13  98  67  0   127 74
  1234    45  76  2004    67  87  98  5   4   3   77  8   99  76  56  4   3   2   65  78  44  53  67  98  79  53  23  65
  </code></pre>
  
  <p>I'm trying to make it appear as below (column R represents Rested and column A represents Active. and monthly JR, FR, MR respectively means Jan Rested, Feb Rested, Mar Rested and JA, FA, MA respectively means Jan Active, Feb Active, Mar Active and etc):</p>
  
  <p>So, here I'm trying to convert each of the monthly columns to rows and keeping them beside each other for R and A values by creating a new Month column.</p>
  
  <pre><code> ID     L1  L2  Year    Month   R   A
  1234    89  65  2003    Jan     11  76
  1234    89  65  2003    Feb     34  86
  1234    89  65  2003    Mar     6   30
  1234    89  65  2003    Apr     7   76
  1234    89  65  2003    May     8   43
  1234    89  65  2003    Jun     90  67
  1234    89  65  2003    Jul     65  13
  1234    89  65  2003    Aug     54  98
  1234    89  65  2003    Sep     3   67
  1234    89  65  2003    Oct     22  0
  1234    89  65  2003    Nov     55  127
  1234    89  65  2003    Dec     66  74
  1234    45  76  2004    Jan     67  3
  1234    45  76  2004    Feb     87  2
  1234    45  76  2004    Mar     98  65
  1234    45  76  2004    Apr     5   78
  1234    45  76  2004    May     4   44
  1234    45  76  2004    Jun     3   53
  1234    45  76  2004    Jul     77  67
  1234    45  76  2004    Aug     8   98
  1234    45  76  2004    Sep     99  79
  1234    45  76  2004    Oct     76  53
  1234    45  76  2004    Nov     56  23
  1234    45  76  2004    Dec     4   65
  </code></pre>
  
  <p>I've tried various things like <code>stack</code>,<code>melt</code>,<code>unlist</code> </p>
  
  <pre><code>data_reshape &lt;- reshape(df,direction="long", varying=list(c("JR", "FR", "MR", "AR", "MYR", "JR", "JLR", "AGR", "SR", "OR", "NR", "DR", "JA", "FA","MA", "AA", "MYA", "JA", "JLA","AGA", "SA", "OA","NA", "DA")), v.names="Precipitation", timevar="Month")
  
  data_stacked &lt;- stack(data, select = c("JR", "FR", "MR", "AR", "MYR", "JR", "JLR", "AGR", "SR", "OR", "NR", "DR", "JA", "FA","MA", "AA", "MYA", "JA", "JLA","AGA", "SA", "OA","NA", "DA"))
  </code></pre>
  
  <p>but their result is not quite expected - they are giving Jan values of all years, and then Feb values of all years, and then March values of all years, and etc. But I want to structure them in an proper monthly manner for each Year for each ID existing in the entire dataset.</p>
  
  <p>How to achieve this in R?</p>