0

So I've been hunting around for a solution and am surprised that no one else has a similar ask to mine so here goes nothing.

I have a data frame with 6 columns, 3 of which are factors of various levels and the rest are numeric as follows:

  Profile Type HD June July  Aug 
   Top     X   Y   3     5     8 
   Bot     Y   Y   10    11    5   
   Bot     Z   N   1     7    15 
   Top     S   N   4     6    19  

I am trying to create a new data frame with each cost/month value as its own row, so the data frame above would transform to this:

Month Cost Profile Type HD
 June   3    Top    X   Y 
 June  10    Bot    Y   Y
 June   1    Bot    Z   N
 June   4    Top    S   N
 July   5    Top    X   Y
 July   11   Bot    Y   Y
 July   7    Bot    Z   N
 July   6    Top    S   N
 Aug    8    Top    X   Y
 Aug    5    Bot    Y   Y
 Aug    15   Bot    Z   N
 Aug    19   Top    S   N

My thinking is to make two data frames, with the left having my month, cost columns and left join; or, perhaps loop through each month column and create a new 2 column data frame. But, I am new to R so any help would be greatly appreciated.

Sam Firke
  • 21,571
  • 9
  • 87
  • 105
Dom
  • 187
  • 3
  • 14
  • for your particular example: `library(tidyr); dat %>% gather(key, val, -c(Profile, Type, HD))` – jeremycg Dec 10 '15 at 16:46
  • I don't think this is a duplicate of that question. The linked Q goes from long to wide, this is the opposite. – Sam Firke Dec 10 '15 at 17:59

0 Answers0