I have a data.frame that looks like this:
ID Date.A Date.B Variable
A 01/01/2012 03/24/2012 Apples
A 02/01/2012 03/24/2012 Oranges
B 01/01/2012 02/04/2012 Bananas
C 01/01/2012 04/22/2012 Apples
A 03/01/2012 03/24/2012 Grapes
B 02/01/2012 02/04/2012 Oranges
D 01/01/2012 03/15/2012 Apples
For each ID, I need to find the Variable which corresponds to the first of the month previous to Date.B. So for ID A, I need to extract “Oranges”, because 03/24/2012 – 1 month = 02/24/2012, and the first of that month is 02/01/2012.
I have no problem with the date calculations using lubridate: format.Date(df$Date.B – months(1), format=”%Y-%m-01″). I’m having trouble incorporating that logic into ddply or in a programmatic manner without writing a nasty for loop. Not asking for the algorithmic help or for someone to solve it for me, but would love to see an example of ddply that uses custom date logic similar to this.
Thanks in advance.
This is not with
lubridateorplyrand I’m not even sure if it’s the solution to your problem. I chose to use themondatepackage for the reasons tcash21 states:And I don’t use plyr much any more so I don’t really think in terms of plyr that much. if your data is called dat:
Which yields:
Clearly there’s an NA for some months for the reason in the comment I attached to your question.