0

the data frame looks like below.

Cust   Month     Amount

cust1  Jan 2015  100
cust1  Feb 2015  200
cust1  Mar 2015  300
cust1  Apr 2015  400
cust2  Mar 2015  800
cust2  Apr 2015  900

i need in the below format.

Cust  Jan 2015 Feb 2015 Mar 2015 Apr 2015

cust1   100      200      300       400
cust2   na        na      800       900

Note : Yearmon should be in order while reshaping. if the customer has no data for a particular month. then it must be replaced with zero.

please suggest me on how to go ahead. i tried using reshape function, but yearmon is not in order.

i want the month column to be in order for forecasting customer amount

Thanks in advance.

Prathap Kb
  • 35
  • 5
  • 1
    @ Jaap. it did not work as i have a yearmon column...not a duplicate imo. – Prathap Kb Feb 20 '16 at 16:05
  • This is an exact duplicate – Pierre L Feb 20 '16 at 16:17
  • I think what OP is asking for is how to control the ordering of the columns upon reshaping. The link for the "duplicate" doesn't appear to address this (that is not to say there isn't a link elsewhere that does). The OP could make the `Month` column an ordered factor before the reshaping as this would likely keep the ordering; or the column ordering could be adjusted after reshaping as well. – steveb Feb 20 '16 at 16:30
  • The reshaping is an duplicate. Ordering the columns is another question. If the ordered columns are given by a vector `m.y` one can achieve the desired result by `reshaped.dataframe[, m.y]`. – jogo Feb 20 '16 at 16:39

0 Answers0